Found 34734 Articles for Programming

C# Program to return the difference between two sequences

Ankith Reddy
Updated on 23-Jun-2020 08:34:54

173 Views

Set two sequences.double[] arr1 = { 10.2, 15.6, 23.3, 30.5, 50.2 }; double[] arr2 = { 15.6, 30.5, 50.2 };To get the difference between both the above arrays, use Except() method.IEnumerable res = arr1.AsQueryable().Except(arr2);The following is the complete code.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       double[] arr1 = { 10.2, 15.6, 23.3, 30.5, 50.2 };       double[] arr2 = { 15.6, 30.5, 50.2 };       Console.WriteLine("Initial List...");       foreach(double ele in arr1) {          Console.WriteLine(ele);       }       IEnumerable res = arr1.AsQueryable().Except(arr2);       Console.WriteLine("New List...");       foreach (double a in res) {          Console.WriteLine(a);       }    } }OutputInitial List... 10.2 15.6 23.3 30.5 50.2 New List... 10.2 23.3

C# Enum TryParse() Method

karthikeya Boyini
Updated on 23-Jun-2020 08:36:02

8K+ Views

The TryParse() method converts the string representation of one or more enumerated constants to an equivalent enumerated object.Firstly, set an enum.enum Vehicle { Bus = 2, Truck = 4, Car = 10 };Now, let us declare a string array and set some values.string[] VehicleList = { "2", "3", "4", "bus", "Truck", "CAR" };Now parse the values accordingly using Enum TryParse() method.Example Live Demousing System; public class Demo {    enum Vehicle { Bus = 2, Truck = 4, Car = 10 };    public static void Main() {       string[] VehicleList = { "2", "3", "4", "bus", "Truck", "CAR" ... Read More

C# Queryable SkipWhile() Method

Arjun Thakur
Updated on 23-Jun-2020 08:36:30

103 Views

Bypass elements in an array and return the remaining elements using the SkipWhile() method.The following is our array −int[] marks = { 45, 88, 55, 90, 95, 85 };Now, let’s skip the elements greater than or equal to 60. The condition we have set using Lambda Expression.IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipWhile(s => s >= 60);Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] marks = { 45, 88, 55, 90, 95, 85 };       // skips elements above 60       IEnumerable selMarks = ... Read More

C# Linq SkipLast Method

Samual Sam
Updated on 23-Jun-2020 08:36:50

1K+ 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

Chandu yadav
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

George John
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

karthikeya Boyini
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

Samual Sam
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

Ankith Reddy
Updated on 23-Jun-2020 08:26:45

268 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

karthikeya Boyini
Updated on 23-Jun-2020 08:27:09

1K+ 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

Advertisements