FomatException is thrown when the format of an argument is invalid.Let us see an example.When we set a value other than int to int.Parse() method, then FormatException is thrown as shown below −Example Live Demousing System; class Demo { static void Main() { string str = "3.5"; int res = int.Parse(str); } }The following error is thrown when the above program is compiled since we have passed a value other than integer.OutputUnhandled Exception: System.FormatException: Input string was not in a correct format.
Get the difference between two arrays using the Except() method.The following are the two arrays.int[] arr = { 9, 12, 15, 20, 35, 40, 55, 67, 88, 92 }; int[] arr2 = { 20, 35 };To get the difference, use Except() method that returns the first list, except the elements in the second list.arr.AsQueryable().Except(arr2);The following is the entire example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Program { static void Main() { int[] arr = { 5, 10, 15, 20, 35, 40 }; int[] except = { 20, 35 }; ... Read More
The ElementAt() method in C# to get elements at specified index position.Firstly, set the string array.string[] str = { "Jack", "Pat", "David"};Now, to get the element at a particular index, use the ElementAt() method as shown in the following example −Example Live Demousing System.IO; using System; using System.Linq; class Program { static void Main() { string[] str = { "Jack", "Pat", "David"}; Random r = new Random(DateTime.Now.Second); // to generate random string string res = str.AsQueryable().ElementAt(r.Next(0, str.Length)); Console.WriteLine("Random Name = '{0}'", res); } }OutputRandom Name = 'Jack'
Use the First() method to get the first element from an array.Firstly, set an array.int[] arr = {20, 40, 60, 80 , 100};Now, use the Queryable First() method to return the first element.arr.AsQueryable().First();The following is the entire example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Program { static void Main() { int[] arr = {20, 40, 60, 80 , 100}; // getting the first element int res = arr.AsQueryable().First(); Console.WriteLine(res); } }Output20
The Sortable standard format specifier represents a custom date and time format string.The format string is defined by the DateTimeFormatInfo.SortableDateTimePattern property.The custom format string.yyyy'-'MM'-'dd'T'HH':'mm':'ssExample Live Demousing System; class Demo { static void Main() { DateTime date = new DateTime(2018, 9, 5, 2, 12, 40); Console.WriteLine(date.ToString("s")); } }Output2018-09-05T02:12:40
To check for an element in a string, use the Contains() method.The following is our string array.string[] arr = { "Java", "C++", "Python"};Now, use Contains() method to find a specific string in the string array.arr.AsQueryable().Contains(str);Let us see the complete example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo { static void Main() { string[] arr = { "Java", "C++", "Python"}; string str = "Python"; bool res = arr.AsQueryable().Contains(str); Console.WriteLine("Array has Python? "+res); } }OutputArray has Python? True
Use Linq Contains() method to search for as specific string in an array of strings.string[] arr = { "Bag", "Pen", "Pencil"};Now, add the string in a string variable i.e. the string you want to search.string str = "Pen";Use the Contains() method to search the above string.arr.AsQueryable().Contains(str);Let us see the entire example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo { static void Main() { string[] arr = { "Bag", "Pen", "Pencil"}; string str = "Pen"; bool res = arr.AsQueryable().Contains(str); Console.WriteLine("String Pen is in the array? "+res); ... Read More
The Count method returns the count of elements in a sequence.Let us first set an array.string[] arr = { "Java", "C++", "Python"};Now, use the Count() method to count the array elements.arr.AsQueryable().Count();The following is the complete example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo { static void Main() { string[] arr = { "Java", "C++", "Python"}; int arr_count = arr.AsQueryable().Count(); Console.WriteLine("Count of arrays: {0}", arr_count); } }OutputCount of arrays: 3
This method is used to handle empty collections. Instead of showing an error, this method displays a default value.We have the following list.List myList = new List();As you can see, since the above list is empty, we can display the default value.var res = myList.DefaultIfEmpty();Let us see an example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo { static void Main() { List myList = new List(); var res = myList.DefaultIfEmpty(); foreach (var a in res) { Console.WriteLine(a); } } }Output0
To get the Upperbound and Lowerbound, use the GetUpperBound() GetLowerBound() methods in C#, respectively.The parameter to be set under these methods is the dimensions i.e.Let’s say our 3D array is −int[, , ] arr = new int[2, 3, 4];For a three-dimensional arrays, dimension 0.arr.GetUpperBound(0) arr.GetLowerBound(0)For a three-dimensional arrays, dimension 1.arr.GetUpperBound(1) arr.GetLowerBound(1)For a three-dimensional arrays, dimension 2.arr.GetUpperBound(2) arr.GetLowerBound(2)Example Live Demousing System; class Program { static void Main() { int[, , ] arr = new int[2, 3, 4]; Console.WriteLine("Dimension 0 Upper Bound: {0}", arr.GetUpperBound(0).ToString()); Console.WriteLine("Dimension 0 Lower Bound: {0}", arr.GetLowerBound(0).ToString()); ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP