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 814 of 2109
Aggregate method in C#
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 MoreAll Method in C#
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 MoreFind a specific element in a C# List
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 MoreToDictionary method in C#
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 MoreContainsValue in C#
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 MoreCase-insensitive Dictionary in C#
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 MoreC# Program to count the number of lines in a file
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 MoreC# Program to write an array to a file
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 MoreC# Program to check whether a directory exists or not
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 MoreC# Program to display the name of the directory
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