Csharp Articles

Page 101 of 196

C# program to convert string to long

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 15K+ Views

To convert a string to a long, use the Long.parse method in C# −Firstly, set a string −string str = "6987646475767";Now, convert it to long −long.Parse(str);Here is the complete code −Exampleusing System; using System.Linq; class Demo {    static void Main() {       string str = "6987646475767";       long res = long.Parse(str);       Console.WriteLine(res);    } }Output6987646475767

Read More

Sort KeyValuePairs in C#

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 950 Views

Use the Sort method to sort the KeyValuePairs collection.Firstly, set the collection −var myList = new List(); // adding elements myList.Add(new KeyValuePair(1, 20)); myList.Add(new KeyValuePair(2, 15)); myList.Add(new KeyValuePair(3, 35)); myList.Add(new KeyValuePair(4, 50)); myList.Add(new KeyValuePair(5, 25));To sort, use the Sort() method. With that, we have used the CompareTo() method to compare values −myList.Sort((x, y) => (y.Value.CompareTo(x.Value)));The following is the complete code −Exampleusing System; using System.Collections.Generic; class Program {    static void Main() {       var myList = new List();       // adding elements       myList.Add(new KeyValuePair(1, 20));       myList.Add(new KeyValuePair(2, 15));   ...

Read More

KeyNotFoundException in C#

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

The KeyNotFoundException is thrown when a key you are finding is not available in the Dictionary collection.Let us see an example −Exampleusing System; using System.Collections.Generic; public class Demo {    public static void Main() {       try {          var dict = new Dictionary() {             {"TV", "Electronics"},             {"Laptop", "Computers"},          };          Console.WriteLine(dict["Pen Drive"]);       }       catch (Exception e) {          Console.WriteLine(e);       }    } }The ...

Read More

C# orderby Keyword

George John
George John
Updated on 11-Mar-2026 247 Views

Use the orderby leyword to sort a list in ascending or descending order.The following is the list −List myList = new List(); myList.Add("truck"); myList.Add("bus"); myList.Add("bicycle"); myList.Add("motorbike");Now let us sort the list in descending order −myLen = from element in myList orderby element.Length descending select element;Here is the complete code −Exampleusing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       List myList = new List();       myList.Add("truck");       myList.Add("bus");       myList.Add("bicycle");       myList.Add("motorbike");       var myLen = from element in myList       ...

Read More

Get all drives in C#

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 721 Views

Firstly, use GetDrives to get the name of all the drives −var drv = DriveInfo.GetDrives();Loop through to get the name of all the drives on the system −foreach (DriveInfo dInfo in drv) {    Console.WriteLine(dInfo.Name); }Let us see the complete code −Exampleusing System; using System.Linq; using System.IO; public class Demo {    public static void Main() {       var drv = DriveInfo.GetDrives();       foreach (DriveInfo dInfo in drv) {          Console.WriteLine(dInfo.Name);       }    } }Output/etc/resolv.conf /etc/hostname /etc/hosts /run/secrets /home/cg/rootNote: The result will vary on different Operating Systems. The above output ...

Read More

TakeWhile method in C# ()

Ankith Reddy
Ankith Reddy
Updated on 11-Mar-2026 507 Views

With the TakeWhile() method, you can get methods by setting a condition base on Predicate.Firstly, declare and initialize an array −int[] arr = { 25, 40, 65, 70};Now, use the TakeWhile() method and predicate to get all the elements that are less than 30.var val = arr.TakeWhile(ele => ele < 30);Let us see the same example, wherein we have displayed the values less than 30 using Predicate −Exampleusing System; using System.Linq; using System.IO; public class Demo {    public static void Main() {       int[] arr = { 25, 40, 65, 70};       var val = arr.TakeWhile(ele => ele < 30);       foreach (int res in val) {          Console.WriteLine(res);       }    } }Output25

Read More

Aggregate method in C#

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

Use the Aggregate method in C# to perform mathematical operations such as Sum, Min, Max, Average, etc.Let us see an example to multiply array elements using Aggregate method.Here is our array −int[] arr = { 10, 15, 20 };Now, use Aggregate() method −arr.Aggregate((x, y) => x * y);Here is the complete code −Exampleusing System; using System.Linq; using System.IO; public class Demo {    public static void Main() {       int[] arr = { 10, 15, 20 };       // Multiplication       int res = arr.Aggregate((x, y) => x * y);       Console.WriteLine(res);    } }Output3000

Read More

All Method in C#

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

The All() extension method is part of the System.Linq namepspace. Using this method, you can check whether all the elements match a certain condition or not.Set an array −int[] arr = { 6, 7, 15, 40, 55 };The following is an example. It checks whether all the elements in the array is greater than and equal to 2 or not −arr.All(element => element > = 2);Here is the complete code −Exampleusing System; using System.Linq; class Program {    static void Main() {       int[] arr = { 6, 7, 15, 40, 55 };       bool res = arr.All(element => element >= 2);       Console.WriteLine(res);    } }OutputTrue

Read More

Find a specific element in a C# List

Samual Sam
Samual Sam
Updated on 11-Mar-2026 330 Views

Set a list −List myList = new List() {    5,    10,    17,    19,    23,    33 };Let us say you need to find an element that is divisible by 2. For that, use the Find() method −int val = myList.Find(item => item % 2 == 0);Here is the complete code −Exampleusing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       List myList = new List() {          5,          10,          17,          19,          23,          33       };       Console.WriteLine("List: ");       foreach(int i in myList) {          Console.WriteLine(i);       }       int val = myList.Find(item => item % 2 == 0);       Console.WriteLine("Element that divides by zero: "+val);    } }OutputList: 5 10 17 19 23 33 Element that divides by zero: 10

Read More

ToDictionary method in C#

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 2K+ Views

The ToDictionary method is an extension method in C# and converts a collection into Dictionary.Firstly, create a string array −string[] str = new string[] {"Car", "Bus", "Bicycle"};Now, use the Dictionary method to convert a collection to Dictionary −str.ToDictionary(item => item, item => true);Here is the complete code −Exampleusing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       string[] str = new string[] {"Car", "Bus", "Bicycle"};       // key and value under ToDictionary       var d = str.ToDictionary(item => item, item => true);       foreach (var ele in ...

Read More
Showing 1001–1010 of 1,951 articles
« Prev 1 99 100 101 102 103 196 Next »
Advertisements