Found 34489 Articles for Programming

C# Linq SkipLast Method

Naveen Singh
Updated on 23-Jun-2020 08:36:50

2K+ Views

Skip elements from the end and return the remaining elements using the SkipLast() method.The following is an array.int[] marks = { 45, 88, 50, 90, 95, 85 };Now, let us skip two elements from the end using SkipLast() and Lambda Expressions, but this is done after arranging the elements in descending order.IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipLast(2);Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] marks = { 45, 88, 50, 90, 95, 85 };       IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipLast(2);       Console.WriteLine("Skipped ... Read More

C# Numeric (“N”) Format Specifier

Naveen Singh
Updated on 23-Jun-2020 08:24:48

2K+ Views

The numeric ("N") format specifier converts a number to a string of the following form −"-d,ddd,ddd.ddd…"Above,"-" is a negative number symbol if required,"d" is a digit (0-9), "," indicates a group separator,"." is a decimal point symbolExample Live Demousing System; using System.Globalization; class Demo {    static void Main() {       double val1 = -5566.789;       Console.WriteLine(val1.ToString("N", CultureInfo.InvariantCulture));       int val2 = 87987766;       Console.WriteLine(val2.ToString("N3", CultureInfo.InvariantCulture));    } }Output-5,566.79 87,987,766.000

C# Linq Skip() Method

Naveen Singh
Updated on 23-Jun-2020 08:25:44

2K+ Views

Skip elements and return the remaining elements using the Skip() method.The following is an array.int[] marks = { 80, 55, 79, 99 };Now, let us skip 2 elements using Lambda Expressions, but this is done after arranging the elements in descending order.IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).Skip(2);Exampleusing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] marks = { 80, 55, 79, 99 };       IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).Skip(2);       Console.WriteLine("Skipped the result of 1st two students...");       foreach (int res in selMarks) {          console.WriteLine(res);       }    } }

Removing whitespaces using C# Regex

Naveen Singh
Updated on 23-Jun-2020 08:25:19

1K+ Views

Let’s say we want to remove whitespace from the following string str1.string str1 = "Brad Pitt";Now, use Regex Replace to replace whitespace with empty. Here, we have used System.Text.RegularExpressions.string str2 = System.Text.RegularExpressions.Regex.Replace(str1, @"\s+", "");Let us see the complete example.Example Live Demousing System; using System.Text.RegularExpressions; namespace Demo {    class Program {       static void Main(string[] args) {          string str1 = "Brad Pitt";          Console.WriteLine(str1);          string str2 = System.Text.RegularExpressions.Regex.Replace(str1, @"\s+", "");          Console.WriteLine(str2);       }    } }OutputBrad Pitt BradPitt

C# SingleorDefault() Method

Naveen Singh
Updated on 23-Jun-2020 08:26:22

3K+ Views

The method returns a single specific element of a sequence. If the element is not present in the sequence, then the default value is returned.We have two string arrays here.string[] str1 = { "one" }; string[] str2 = { };First array is checked for a single element, whereas the second array is empty and checked using SingleorDefault.str2.AsQueryable().SingleOrDefault();The following is an example showing the usage of SingleorDefault() method.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       string[] str1 = { "one" };       string[] str2 = { }; ... Read More

C# Program to return the only element that satisfies a condition

Naveen Singh
Updated on 23-Jun-2020 08:26:45

283 Views

The Single() method returns the only element that satisfies a condition. If more than one such element is visible, then an error is thrown.The following is our string array.string[] str = { "jack", "tom", "henry", "time"};Now, use the Single() method to get each element. Then, we have used Lambda Expression to calculate an element whose length is greater than four.str.AsQueryable().Single(name => name.Length > 4);Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       string[] str = { "jack", "tom", "henry", "time"};       // finding string whose length is ... Read More

C# Single() Method

Naveen Singh
Updated on 23-Jun-2020 08:27:09

2K+ Views

Get only a single element of a sequence using the Single() method.Let’s say we have a string array with only one element.string[] str = { "one" };Now, get the element.str.AsQueryable().Single();Here is our code.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       string[] str = { "one" };       string res = str.AsQueryable().Single();       Console.WriteLine(res);    } }Outputone

C# Program to read all the lines one by one in a file

Naveen Singh
Updated on 23-Jun-2020 08:27:40

240 Views

Use ReadAllLines() method to read all the lines one by one in a file.Let’s say we have a file “new.txt” with the following lines.One Two ThreeFirstly, set the path of the file to be read.String myPath = "new.txt";Now add it under a string array to fetch the lines on by one.String[] fLine = File.ReadAllLines(myPath);Let’s say you need to fetch the first line. For that.fLine[0]The following is the complete example that reads lines one by one in a file.Exampleusing System; using System.IO; public class Demo {    public static void Main() {       String myPath = "new.txt";     ... Read More

LinkedList AddLast method in C#

Naveen Singh
Updated on 23-Jun-2020 08:29:12

302 Views

Declare a string array.string [] students = {"Jenifer", "Angelina", "Vera"};Add it to a LinkedList.string [] students = {"Jenifer", "Angelina", "Vera"};Now, use AddLast() method to add a node at the end.list.AddLast("Anne");Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       string [] students = {"Jenifer", "Angelina", "Vera"};       LinkedList list = new LinkedList(students);       foreach (var stu in list) {          Console.WriteLine(stu);       }       // add a node at the end       Console.WriteLine("Node added at the last...");       list.AddLast("Anne"); ... Read More

C# Queryable SequenceEqual() Method

Naveen Singh
Updated on 23-Jun-2020 08:29:40

85 Views

To check whether two sequences are equal or not, use the SequenceEqual() method.Firstly, set the sequences.Employee emp1 = new Employee { EmployeeRank = 4, EmpName = "Amit", EmpMarks = 90 }; Employee emp2 = new Employee { EmployeeRank = 5, EmpName = "Raman", EmpMarks = 95 }; List employee1 = new List { emp1, emp2 }; List employee2 = new List { emp1, emp2 };Now, find whether the sequences are equal or not.employee1.AsQueryable().SequenceEqual(employee2);Here is the example that shows the result.Exmaple Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       Employee ... Read More

Advertisements