Found 34494 Articles for Programming

C# Program to get the name of the drive

Ankith Reddy
Updated on 22-Jun-2020 14:33:38

146 Views

Set the drive for which you want to display the name −DriveInfo info = new DriveInfo("C");Now, to get the drive name, use the Name property −info.NameHere is the complete code with output −Exampleusing System; using System.Linq; using System.IO; public class Demo {    public static void Main() {       DriveInfo info = new DriveInfo("C");       Console.WriteLine(“Drive: “+info.Name);    } }OutputThe following is the output −D:/

C# Program to get free space in a drive

Arjun Thakur
Updated on 06-Apr-2020 11:01:50

541 Views

To get the free space in a drive, use the AvailableFreeSpace and TotalFreeSpace properties in C#.Firstly, set the name of the drive using DriveInfo −DriveInfo dInfo = new DriveInfo("D");Let’s say, you need to find the available space for D drive −Exampleusing System; using System.Linq; using System.IO; public class Demo {    public static void Main() {       DriveInfo dInfo = new DriveInfo("D");       // Get free space       Console.WriteLine(dInfo.AvailableFreeSpace);       // get total free space       Console.WriteLine(dInfo.TotalFreeSpace);    } }OutputThe following is the output showing the free space available in D drive−722243567912 722243567912

C# Program to display the last three elements from a list in reverse order

Chandu yadav
Updated on 22-Jun-2020 14:19:58

1K+ Views

To display the last three elements from a list, use the Take() method. For reversing it, use the Reverse() method.Firstly, declare a list and add elements to it −List myList = new List(); myList.Add("One"); myList.Add("Two"); myList.Add("Three"); myList.Add("Four");Now, use the Take() method with Reverse() to display the last three elements from the list in reverse order −myList.Reverse().Take(3);The following is the code −Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       List myList = new List();       myList.Add("One");       myList.Add("Two");       myList.Add("Three");       ... Read More

C# orderby Keyword

George John
Updated on 22-Jun-2020 14:26:38

133 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 −Example Live Demousing 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

KeyNotFoundException in C#

Chandu yadav
Updated on 22-Jun-2020 14:27:28

437 Views

The KeyNotFoundException is thrown when a key you are finding is not available in the Dictionary collection.Let us see an example −Example Live Demousing 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);       }    } ... Read More

Sort KeyValuePairs in C#

Arjun Thakur
Updated on 22-Jun-2020 14:28:04

600 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 −Example Live Demousing 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

C# Program to find the smallest element from an array

Ankith Reddy
Updated on 22-Jun-2020 14:14:43

2K+ Views

Declare an array −int[] arr = { 5, 9, 2, 7 };Now to get the smallest element from an array, use the Min() method −arr.Min());Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] arr = { 5, 9, 2, 7 };       Console.WriteLine(arr.Min());    } }Output2

KeyValuePair in C#

George John
Updated on 22-Jun-2020 14:15:06

10K+ Views

The KeyValuePair class stores a pair of values in a single list with C#.Set KeyValuePair and add elements −var myList = new List(); // adding elements myList.Add(new KeyValuePair("Laptop", 20)); myList.Add(new KeyValuePair("Desktop", 40)); myList.Add(new KeyValuePair("Tablet", 60));Here is the code to learn how to work with KeyValuePair and display the keys and values −ExampleUsing System; using System.Collections.Generic; class Program {    static void Main() {       var myList = new List();       // adding elements       myList.Add(new KeyValuePair("Laptop", 20));       myList.Add(new KeyValuePair("Desktop", 40));       myList.Add(new KeyValuePair("Tablet", 60));       foreach (var val in myList) {          Console.WriteLine(val);       }    } }

C# program to convert string to long

Chandu yadav
Updated on 22-Jun-2020 14:15:30

12K+ 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 −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       string str = "6987646475767";       long res = long.Parse(str);       Console.WriteLine(res);    } }Output6987646475767

Convert string to bool in C#

Arjun Thakur
Updated on 22-Jun-2020 14:15:52

2K+ Views

To convert a string to a bool, use the Bool.parse method in C# −Firstly, set a string −string str = "false";Now, convert it to bool −bool.Parse(str);Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       string str = "false";       bool res = bool.Parse(str);       Console.WriteLine(res);    } }OutputFalse

Advertisements