Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 813 of 2109
KeyValuePair in C#
The KeyValuePair struct in C# stores a pair of values in a single object, where one value acts as the key and the other as the value. It is commonly used in collections like List and as the element type in dictionaries. KeyValuePair is particularly useful when you need to associate two related pieces of data together without creating a custom class or when working with dictionary enumerations. Syntax Following is the syntax for creating a KeyValuePair − KeyValuePair pair = new KeyValuePair(key, value); Following is the syntax for accessing key and value ...
Read MoreSort KeyValuePairs in C#
The KeyValuePair structure in C# represents a key-value pair that can be stored in collections. When working with lists of KeyValuePairs, you often need to sort them based on either the key or value. C# provides several approaches to accomplish this sorting. Syntax Following is the syntax for sorting KeyValuePairs using the Sort() method with a lambda expression − myList.Sort((x, y) => x.Value.CompareTo(y.Value)); // Sort by Value (ascending) myList.Sort((x, y) => y.Value.CompareTo(x.Value)); // Sort by Value (descending) myList.Sort((x, y) => x.Key.CompareTo(y.Key)); // Sort by Key (ascending) Following ...
Read MoreKeyNotFoundException in C#
The KeyNotFoundException in C# is thrown when you attempt to access a key that does not exist in a Dictionary collection. This exception occurs specifically when using the indexer syntax dictionary[key] to retrieve a value. When KeyNotFoundException Occurs This exception is thrown in the following scenarios − Accessing a Dictionary using dict[key] where the key doesn't exist Using the indexer on collections like SortedDictionary or SortedList with non-existent keys Attempting to retrieve values from key-value collections without checking key existence Dictionary Key Access ...
Read MoreC# orderby Keyword
The orderby keyword in C# is used to sort data in LINQ queries. It allows you to sort collections in either ascending or descending order based on one or more criteria. Syntax Following is the syntax for using orderby in LINQ queries − from element in collection orderby element ascending select element; For descending order − from element in collection orderby element descending select element; Multiple sorting criteria can be applied using commas − from element in collection orderby element.Property1, element.Property2 descending select element; Using orderby ...
Read MoreC# Program to get free space in a drive
To get the free space in a drive, use the AvailableFreeSpace and TotalFreeSpace properties of the DriveInfo class in C#. This is useful for monitoring disk space and making decisions based on available storage. Syntax Following is the syntax for creating a DriveInfo object and accessing free space properties − DriveInfo driveInfo = new DriveInfo("DriveLetter"); long availableSpace = driveInfo.AvailableFreeSpace; long totalFreeSpace = driveInfo.TotalFreeSpace; Properties Property Description AvailableFreeSpace Gets the amount of available free space on a drive, in bytes, available to the current user. ...
Read MoreC# Program to get the name of the drive
The DriveInfo class in C# provides information about drives on the system. You can use the Name property to get the name of a specific drive by passing the drive letter to the DriveInfo constructor. Syntax Following is the syntax for creating a DriveInfo object and getting the drive name − DriveInfo info = new DriveInfo("driveLetter"); string driveName = info.Name; Parameters driveLetter − A string representing the drive letter (e.g., "C", "D", "E"). Return Value The Name property returns a string containing the name of the drive, ...
Read MoreC# Program to get the name of root directory
Use the RootDirectory property to get the name of the root directory of a drive in C#. This property belongs to the DriveInfo class and returns a DirectoryInfo object representing the root directory. Syntax Following is the syntax for getting the root directory − DriveInfo driveInfo = new DriveInfo("driveLetter"); DirectoryInfo rootDir = driveInfo.RootDirectory; Using DriveInfo.RootDirectory Property First, create a DriveInfo object to specify the drive for which you want the root directory − DriveInfo dInfo = new DriveInfo("C"); Then, the RootDirectory property gives you the root directory − ...
Read MoreGet the drive format in C#
The DriveFormat property in C# is used to get the file system format of a drive. It returns a string representing the format type such as NTFS, FAT32, or exFAT for Windows systems. Syntax Following is the syntax for using the DriveFormat property − DriveInfo driveInfo = new DriveInfo(driveLetter); string format = driveInfo.DriveFormat; Parameters driveLetter − A string representing the drive letter (e.g., "C", "D", "E"). Return Value The DriveFormat property returns a string containing the name of the file system, such as "NTFS", "FAT32", or "exFAT". ...
Read MoreGet all drives in C#
In C#, you can retrieve information about all drives on a system using the DriveInfo.GetDrives() method. This method returns an array of DriveInfo objects representing all logical drives on the current system, including hard drives, network drives, CD-ROMs, and other storage devices. Syntax Following is the syntax for getting all drives − DriveInfo[] drives = DriveInfo.GetDrives(); To iterate through the drives and access their properties − foreach (DriveInfo drive in drives) { Console.WriteLine(drive.Name); } Using GetDrives() to Display Drive Names The simplest approach is to get ...
Read MoreTakeWhile method in C# ()
The TakeWhile() method in C# is a LINQ extension method that returns elements from the beginning of a sequence as long as a specified condition is true. It stops immediately when the first element that doesn't satisfy the condition is encountered, even if later elements might satisfy the condition. Syntax Following is the syntax for the TakeWhile() method − public static IEnumerable TakeWhile( this IEnumerable source, Func predicate ) Parameters source − The sequence to return elements from. predicate − ...
Read More