Csharp Articles

Page 157 of 196

C# Program to return the only element that satisfies a condition

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

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 More

C# SingleorDefault() Method

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

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 More

C# Linq Skip() Method

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

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 More

C# Numeric ("N") Format Specifier

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 2K+ Views

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 More

C# Linq SkipLast Method

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

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 More

C# Enum TryParse() Method

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

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 More

C# Program to return the difference between two sequences

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

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 More

C# Linq Sum() Method

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

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

C# Program to find the sum of a sequence

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

A sequence in C# is a collection of elements that can be processed using LINQ methods. To find the sum of a sequence, you can use several approaches including the Sum() method from LINQ, which provides a simple and efficient way to calculate the total. The most common approach is using the LINQ Sum() method, which can be applied to any IEnumerable collection. Syntax Following is the syntax for using the Sum() method − // For numeric collections collection.Sum(); // With selector function collection.Sum(x => x.Property); Using LINQ Sum() Method The ...

Read More

C# Queryable Take() Method

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

The Take() method in C# is a LINQ extension method that returns a specified number of contiguous elements from the start of a sequence. It's commonly used with IQueryable collections to limit the number of results returned from a query. Syntax Following is the syntax for the Take() method − public static IQueryable Take( this IQueryable source, int count ) Parameters source − The IQueryable to return elements from. count − The number of elements to return. Return Value ...

Read More
Showing 1561–1570 of 1,951 articles
« Prev 1 155 156 157 158 159 196 Next »
Advertisements