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 832 of 2109
C# Program to read all the lines one by one in a file
The File.ReadAllLines() method in C# reads all lines from a file and returns them as a string array. This method is useful when you need to process a file line by line or access specific lines by their index. Syntax Following is the syntax for File.ReadAllLines() method − string[] lines = File.ReadAllLines(filePath); Parameters filePath − The path to the file to be read (string) Return Value Returns a string[] array containing all lines from the file. Each element represents one line from the file. Using ReadAllLines() to Read ...
Read MoreC# Single() Method
The Single() method in C# returns the only element of a sequence. It throws an exception if the sequence is empty or contains more than one element, making it useful when you expect exactly one element. The Single() method is available in both LINQ to Objects and LINQ to Entities through the System.Linq namespace. Syntax Following is the syntax for the Single() method − public static TSource Single(this IEnumerable source); public static TSource Single(this IEnumerable source, Func predicate); Parameters source − The IEnumerable to return the single element from. predicate − ...
Read MoreC# Program to return the only element that satisfies a condition
The Single() method in C# returns the only element from a sequence that satisfies a specified condition. If no element or more than one element satisfies the condition, it throws an InvalidOperationException. This method is part of LINQ and is commonly used when you expect exactly one element to match your criteria. Syntax Following is the syntax for the Single() method − // Without condition - returns the only element public static T Single(this IEnumerable source) // With condition - returns the only element that matches the predicate public static T Single(this IEnumerable source, ...
Read MoreC# SingleorDefault() Method
The SingleOrDefault() method in C# returns a single specific element from a sequence. If no element is found, it returns the default value for the type. If multiple elements are found, it throws an exception. This method is part of LINQ and can be used with any IEnumerable collection. It's particularly useful when you expect exactly zero or one element in the result. Syntax Following is the basic syntax for SingleOrDefault() − public static TSource SingleOrDefault(this IEnumerable source) With a predicate condition − public static TSource SingleOrDefault(this IEnumerable source, Func predicate) ...
Read MoreC# Linq Skip() Method
The Skip() method in C# LINQ is used to skip a specified number of elements from the beginning of a sequence and return the remaining elements. This method is particularly useful when you need to bypass certain elements in a collection before processing the rest. Syntax Following is the syntax for the Skip() method − public static IEnumerable Skip( this IEnumerable source, int count ) Parameters source − The sequence to return elements from. count − The number of elements ...
Read MoreC# Numeric ("N") Format Specifier
The numeric (N) format specifier converts a number to a string with thousands separators and decimal places. It follows the pattern -d, ddd, ddd.ddd… where the minus sign appears for negative numbers, digits are grouped with commas, and decimal places are included. Syntax Following is the syntax for using the numeric format specifier − number.ToString("N") // Default 2 decimal places number.ToString("N0") // No decimal places number.ToString("Nn") ...
Read MoreC# Linq SkipLast Method
The SkipLast() method in C# LINQ is used to skip a specified number of elements from the end of a sequence and return the remaining elements. This method is particularly useful when you need to exclude the last few items from a collection. Syntax Following is the syntax for the SkipLast() method − public static IEnumerable SkipLast( this IEnumerable source, int count ) Parameters source − The sequence to skip elements from. count − The number of elements to skip from the end ...
Read MoreC# Enum TryParse() Method
The Enum.TryParse() method in C# safely attempts to convert the string representation of one or more enumerated constants to an equivalent enumerated object. Unlike Enum.Parse(), it returns a boolean value indicating success or failure instead of throwing an exception for invalid values. Syntax Following are the overloads for the Enum.TryParse() method − public static bool TryParse(string value, out TEnum result) public static bool TryParse(string value, bool ignoreCase, out TEnum result) Parameters value − The string representation of the enumeration name or underlying value to convert. ignoreCase − A ...
Read MoreC# Program to return the difference between two sequences
In C#, you can find the difference between two sequences using the Except() method from LINQ. This method returns elements from the first sequence that do not appear in the second sequence, effectively performing a set difference operation. Syntax Following is the syntax for using the Except() method − IEnumerable result = firstSequence.Except(secondSequence); Parameters firstSequence − The sequence to return elements from. secondSequence − The sequence whose elements to exclude from the returned sequence. Return Value The method returns an IEnumerable containing the elements from the first sequence that ...
Read MoreC# Linq Sum() Method
The LINQ Sum() method in C# is used to calculate the sum of numeric values in a collection. It works with various numeric types including int, double, decimal, and float, and can also work with nullable types. Syntax Following is the basic syntax for the Sum() method − // For basic numeric collections collection.Sum() // With a selector function collection.Sum(selector) Parameters The Sum() method has the following parameter − selector (optional) − A function to extract numeric values from each element. Return Value Returns the sum of ...
Read More