Reverse the elements in an array, using the Reverse method.Here is our character array.char[] ch = { 't', 'i', 'm', 'e' };Now use the Reverse() method with AsQueryable() method to get the reverse.ch.AsQueryable().Reverse();Let us see the complete code.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { char[] ch = { 't', 'i', 'm', 'e' }; Console.Write("String = "); foreach(char arr in ch) { Console.Write(arr); } IQueryable res = ch.AsQueryable().Reverse(); Console.Write("Reversed String = "); foreach (char c in res) { Console.Write(c); } } }OutputString = time Reversed String = emit
Use the BufferWidth gets or sets the width of the buffer area.Use the property like this −Console.BufferWidthLet us see the complete example.Example Live Demousing System; class Demo { static void Main() { Console.WriteLine("Buffer width (columns) = "+Console.BufferWidth); } }OutputBuffer width (columns) = 0
The WindowsTop property is used to gets or set the top position of the console window area relative to the screen buffer.Declare an integer variable to get the top position.int top;Now, use the Console.WindowTop property.top = Console.WindowTop;Let us see the complete example.Example Live Demousing System; class Demo { static void Main() { int top; top = Console.WindowTop; Console.WriteLine("Top position of the Console window = "+top); } }OutputTop position of the Console window = 0
Use Select method and Lambda Expression to calculate the cube of elements.The following is our list.List list = new List { 2, 4, 5, 7 };Now, use the Select() method and calculate the cube.list.AsQueryable().Select(c => c * c * c);The following is the entire example.Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { List list = new List { 2, 4, 5, 7 }; Console.WriteLine("Elements..."); // initial list javascript:void(0) foreach (int n in list) Console.WriteLine(n); ... Read More
Use the Select method to modify the elements in an array.The following is our string array.string[] stationery = { "diary", "board", "pencil", "whiteboard" };The Select method also specifies Lambda Expressions as shown below −Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { string[] stationery = { "diary", "board", "pencil", "whiteboard" }; var res = stationery.AsQueryable().Select((item, index) => new { result = item.Substring(0, index + 4) }); foreach (var str in res) { Console.WriteLine("{0}", str); } } }Output{ result = diar } { result = board } { result = pencil } { result = whitebo }
To cast a specific type to its IEnumerable equivalent, use the AsEnumerable() method. It is an extension method.The following is our array −int[] arr = new int[5]; arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50;Now, get the IEnumerable equivalent.arr.AsEnumerable();Example Live Demousing System; using System.Linq; class Demo { static void Main() { int[] arr = new int[5]; arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50; var res = arr.AsEnumerable(); foreach (var ele in res) { Console.WriteLine(ele); } } }Output10 20 30 40 50
The following are our strings in a list −List list = new List { "keyboard", "mouse", "joystick", "monitor" };To use the first 3 letters, use substring method and use it under the Linq Select method.IEnumerable res = list.AsQueryable() .Cast() .Select(str => str.Substring(0, 3));Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo { static void Main() { List list = new List { "keyboard", "mouse", "joystick", "monitor" }; // getting first 3 letters from every string IEnumerable res = list.AsQueryable() .Cast() .Select(str => str.Substring(0,3)); foreach (string str in res) { Console.WriteLine(str); } } }Outputkey mou joy mon
Use the FirstorDefault() method to return the first element of a sequence or a default value if element isn’t there.The following is our empty list −List val = new List { };Now, we cannot display the first element, since it is an empty collection. For that, use the FirstorDefault() method to display the default value.val.AsQueryable().FirstOrDefault();The following is the complete example.Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo { static void Main() { List val = new List { }; double d = val.AsQueryable().FirstOrDefault(); Console.WriteLine("Default Value = "+d); ... Read More
Find common elements between two arrays using the Intersect() method.The following are our arrays −int[] val1 = { 15, 20, 40, 60, 75, 90 }; int[] val2 = { 17, 25, 35, 55, 75, 90 };To perform intersection.val1.AsQueryable().Intersect(val2);Let us see the entire example.Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo { static void Main() { int[] val1 = { 15, 20, 40, 60, 75, 90 }; int[] val2 = { 17, 25, 35, 55, 75, 90 }; IEnumerable res = val1.AsQueryable().Intersect(val2); Console.WriteLine("Intersection of both the lists..."); foreach (int a in res) Console.WriteLine(a); } }OutputIntersection of both the lists... 75 90
To get the size of a variable, sizeof is used.int x; x = sizeof(int);To get the size of a variable, without using the sizeof, try the following code −// without using sizeof byte[] dataBytes = BitConverter.GetBytes(x); int d = dataBytes.Length;Here is the complete code.Example Live Demousing System; class Demo { public static void Main() { int x; // using sizeof x = sizeof(int); Console.WriteLine(x); // without using sizeof byte[] dataBytes = BitConverter.GetBytes(x); int d = dataBytes.Length; Console.WriteLine(d); } }Output4 4
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP