- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 2628 Articles for Csharp
180 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
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
105 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
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
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
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); } } }
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
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
279 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
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