Csharp Articles

Page 138 of 196

KeyNotFoundException in C#

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 666 Views

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 More

C# orderby Keyword

George John
George John
Updated on 17-Mar-2026 256 Views

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 More

C# Program to get free space in a drive

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 773 Views

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 More

C# Program to get the name of the drive

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 245 Views

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 More

C# Program to get the name of root directory

George John
George John
Updated on 17-Mar-2026 720 Views

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 More

Get the drive format in C#

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 379 Views

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 More

Get all drives in C#

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 744 Views

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 More

TakeWhile method in C# ()

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 515 Views

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

Aggregate method in C#

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

The Aggregate method in C# is a powerful LINQ extension method that performs a sequential operation on all elements in a collection. It applies an accumulator function over a sequence, allowing you to perform custom aggregations like mathematical operations, string concatenations, or complex data transformations. Syntax Following are the main overloads of the Aggregate method − // Basic syntax - uses first element as seed public static TSource Aggregate( this IEnumerable source, Func func ); // With seed value public static TAccumulate Aggregate( ...

Read More

All Method in C#

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 409 Views

The All() extension method is part of the System.Linq namespace. Using this method, you can check whether all the elements in a collection match a certain condition or not. It returns true if all elements satisfy the condition, otherwise false. Syntax Following is the syntax for the All() method − bool result = collection.All(predicate); Parameters predicate − A function to test each element for a condition. It takes an element as input and returns a boolean value. Return Value The All() method returns true if all elements ...

Read More
Showing 1371–1380 of 1,951 articles
« Prev 1 136 137 138 139 140 196 Next »
Advertisements