Csharp Articles

Page 54 of 196

Clear a Hashtable in C#

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 509 Views

Clear a Hashtable, using the Clear() method in C#.The following is our Hashtable −Hashtable h = new Hashtable(); h.Add(1, "Amit"); h.Add(2, "Sachin"); h.Add(3, "Rahul");Use the clear method.h.Clear();If you will now try to display the Hashtable, nothing would get display since the Hashtable is empty.Exampleusing System; using System.Collections; public class Demo {    public static void Main() {       Hashtable h = new Hashtable();       h.Add(1, "Amit");       h.Add(2, "Sachin");       h.Add(3, "Rahul");       Console.WriteLine("Keys and Values list:");       foreach (var key in h.Keys ) {       ...

Read More

C# Program to get distinct element from a sequence

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 337 Views

Set a sequence and add elements.List ID = new List { 120, 111, 250, 111, 120, 300, 399, 450 };Use Distinct() method to get distinct element from the above list.IEnumerable res = ID.AsQueryable().Distinct();Let us see the complete code.Exampleusing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       List ID = new List { 120, 111, 250, 111, 120, 300, 399, 450 };       // get distinct elements       IEnumerable res = ID.AsQueryable().Distinct();       foreach (int arr in res) {          Console.WriteLine(arr);       }    } }Output120 111 250 300 399 450

Read More

How to handle empty collections in C#

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 477 Views

To handle empty collections, use the DefaultIfEmpty() method in C#.If an array is empty, then using this method will show the default method instead of displaying an error.Let’s say we have an empty list.List myList = new List();Now, use DefaultIfEmpty() method to display the default value.myList.DefaultIfEmpty();Exampleusing 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

Read More

Get first three letters from every string in C#

George John
George John
Updated on 11-Mar-2026 3K+ Views

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));Exampleusing 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

Read More

AsEnumerable() in C#

Samual Sam
Samual Sam
Updated on 11-Mar-2026 5K+ Views

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();Exampleusing 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

Read More

C# Linq Intersect Method

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 636 Views

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.Exampleusing 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

Read More

C# Linq FirstorDefault Method

Ankith Reddy
Ankith Reddy
Updated on 11-Mar-2026 20K+ Views

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.Exampleusing 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

AsQueryable() in C#

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 4K+ Views

AsQueryable() method is used to get an IQueryable reference.Let us see an example to find sum of integer values.Firstly, set an integer array.var arr = new int[] { 100, 200, 300, 400 };Now to find the sum, use the Queryable Sum() and AsQueryable() method.Queryable.Sum(arr.AsQueryable());The following is the complete code.Exampleusing System; using System.Linq; class Demo {    static void Main() {       var arr = new int[] { 100, 200, 300, 400 };       int res = Queryable.Sum(arr.AsQueryable());       Console.WriteLine("Sum: "+res);    } }OutputSum: 1000

Read More

C# Queryable Max Method

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 239 Views

Get the maximum value from a sequence using the Max() method.Let’s say the following is our list.List list = new List { 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000 };Use the Max() method to get the largest element.list.AsQueryable().Max();Exampleusing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       List list = new List { 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000 };       foreach(long ele in list)       Console.WriteLine(ele);       // getting largest element       long max_num = list.AsQueryable().Max();       Console.WriteLine("Largest number = {0}", max_num);    } }Output200 400 600 800 1000 1200 1400 1600 1800 2000 Largest number = 2000

Read More

Represent Int32 as a Octal String in C#

George John
George John
Updated on 11-Mar-2026 1K+ Views

To represent Int32 as a Octal string in C#, use the ToString() method and set the base as the ToString() method’s second parameter i.e. 8 for Octal.Int32 represents a 32-bit signed integer.Firstly, set an Int32 variable.int val = 99;Now, convert it to octal string by including 8 as the second parameter.Convert.ToString(val, 8)Exampleusing System; class Demo {    static void Main() {       int val = 99;       Console.WriteLine("Integer: "+val);       Console.Write("Octal String: "+Convert.ToString(val, 8));    } }OutputInteger: 99 Octal String: 143

Read More
Showing 531–540 of 1,951 articles
« Prev 1 52 53 54 55 56 196 Next »
Advertisements