Csharp Articles

Page 139 of 196

Find a specific element in a C# List

Samual Sam
Samual Sam
Updated on 17-Mar-2026 344 Views

In C#, you can find specific elements in a List using various built-in methods. The most common approach is using the Find() method with a lambda expression to specify search criteria. Syntax Following is the syntax for using the Find() method − T element = list.Find(predicate); Where predicate is a lambda expression that returns true for the element you want to find. Using Find() Method The Find() method returns the first element that matches the specified condition. If no element is found, it returns the default value for the type (0 for ...

Read More

ToDictionary method in C#

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

The ToDictionary method is a LINQ extension method in C# that converts any collection into a Dictionary. It allows you to specify how to extract the key and value from each element in the source collection. Syntax Following is the basic syntax for the ToDictionary method − source.ToDictionary(keySelector) source.ToDictionary(keySelector, valueSelector) source.ToDictionary(keySelector, valueSelector, comparer) Parameters keySelector − A function to extract the key from each element. valueSelector − A function to extract the value from each element (optional). comparer − An equality comparer to compare keys (optional). ...

Read More

ContainsValue in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 145 Views

The ContainsValue() method in C# is used to determine whether a Dictionary contains a specific value. It returns true if the value exists in the dictionary, and false otherwise. This method performs a sequential search through all values in the dictionary. Syntax Following is the syntax for using the ContainsValue() method − bool result = dictionary.ContainsValue(value); Parameters value − The value to search for in the dictionary. The type must match the value type of the dictionary. Return Value Returns true if the dictionary contains the specified value; otherwise, ...

Read More

Case-insensitive Dictionary in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 3K+ Views

A case-insensitive Dictionary in C# allows you to perform key lookups without considering the case of string keys. This means keys like "cricket", "CRICKET", and "Cricket" are treated as identical. This is particularly useful when dealing with user input or data from external sources where case consistency cannot be guaranteed. Syntax To create a case-insensitive Dictionary, use the StringComparer.OrdinalIgnoreCase parameter in the constructor − Dictionary dict = new Dictionary(StringComparer.OrdinalIgnoreCase); You can also use other string comparers − // Culture-insensitive comparison Dictionary dict1 = new Dictionary(StringComparer.InvariantCultureIgnoreCase); // Current culture ignore case Dictionary ...

Read More

C# Program to count the number of lines in a file

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

Counting the number of lines in a file is a common task in C# programming. This can be accomplished using several methods from the System.IO namespace. The most straightforward approach is to use the File.ReadAllLines() method combined with the Length property. Using File.ReadAllLines() Method The File.ReadAllLines() method reads all lines from a file into a string array, and then we can use the Length property to count the total number of lines − Example using System; using System.IO; public class Program { public static void Main() { ...

Read More

C# Program to write an array to a file

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

Writing an array to a file in C# can be accomplished using the File.WriteAllLines method from the System.IO namespace. This method writes each array element as a separate line to the specified file. Syntax Following is the syntax for using File.WriteAllLines − File.WriteAllLines(string path, string[] contents); Parameters path − The file path where the array will be written contents − The string array to write to the file Using WriteAllLines Method The WriteAllLines method automatically creates the file if it doesn't exist and overwrites it ...

Read More

C# Program to check whether a directory exists or not

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 343 Views

The Directory.Exists method in C# is used to check whether a specified directory exists on the file system. This method returns a boolean value − true if the directory exists, and false if it does not. This functionality is essential for file system operations where you need to verify directory existence before performing operations like reading files, creating subdirectories, or moving content. Syntax Following is the syntax for the Directory.Exists method − public static bool Exists(string path) Parameters path − A string representing the path to the directory to check. ...

Read More

C# Program to display the name of the directory

Samual Sam
Samual Sam
Updated on 17-Mar-2026 193 Views

In C# programming, you can display the name of a directory using the DirectoryInfo class from the System.IO namespace. This class provides properties and methods to work with directory information, including retrieving just the directory name without the full path. Syntax Following is the syntax for creating a DirectoryInfo object and accessing the directory name − DirectoryInfo dir = new DirectoryInfo(directoryPath); string directoryName = dir.Name; Parameters directoryPath − A string representing the path to the directory Return Value The Name property returns a string containing only the name of ...

Read More

C# Program to get information about a file

Samual Sam
Samual Sam
Updated on 17-Mar-2026 269 Views

To get information about a file means to retrieve the attributes and properties associated with that particular file. File attributes indicate characteristics such as whether a file is normal, hidden, archived, read-only, or a system file. In C#, the FileInfo class provides comprehensive information about files, including attributes, size, creation time, and modification time. Syntax Following is the syntax for creating a FileInfo object and getting file attributes − FileInfo info = new FileInfo("filename.txt"); FileAttributes attr = info.Attributes; Using FileInfo to Get File Attributes The FileAttributes enumeration represents various file attributes that ...

Read More

C# Program to get the last write time of a file

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

To get the last write time of a file in C#, you can use the LastWriteTime property of the FileInfo class. This property returns a DateTime object representing when the file was last modified. You can also use the static File.GetLastWriteTime() method as an alternative approach for retrieving the last write time without creating a FileInfo instance. Syntax Using FileInfo class − FileInfo fileInfo = new FileInfo("filename.txt"); DateTime lastWrite = fileInfo.LastWriteTime; Using File.GetLastWriteTime() method − DateTime lastWrite = File.GetLastWriteTime("filename.txt"); Using FileInfo Class The FileInfo class provides comprehensive file ...

Read More
Showing 1381–1390 of 1,951 articles
« Prev 1 137 138 139 140 141 196 Next »
Advertisements