Csharp Articles

Page 160 of 196

C# Linq Count method

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

The Count method in LINQ returns the number of elements in a sequence. It is one of the most commonly used LINQ methods for determining collection size and can be used with arrays, lists, and any IEnumerable collection. Syntax Following is the syntax for the basic Count method − int count = collection.Count(); Following is the syntax for Count with a predicate condition − int count = collection.Count(predicate); Using Count() on Arrays and Collections The Count method can be applied to any collection that implements IEnumerable. Here's how to ...

Read More

C# DefaultIfEmpty Method

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

The DefaultIfEmpty method in C# is a LINQ extension method used to handle empty collections gracefully. Instead of returning an empty sequence that might cause issues in further operations, it returns a sequence containing a single default value when the original collection is empty. Syntax Following is the syntax for the DefaultIfEmpty method − public static IEnumerable DefaultIfEmpty( this IEnumerable source ) public static IEnumerable DefaultIfEmpty( this IEnumerable source, TSource defaultValue ) Parameters source − The sequence to return ...

Read More

C# OverflowException

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

The OverflowException in C# is thrown when an arithmetic operation results in a value that exceeds the range of the target data type. This commonly occurs during type conversions, arithmetic operations, or when parsing strings that contain values outside the acceptable range. Common Scenarios OverflowException is typically thrown in the following situations − Converting a string to an integer when the value exceeds int.MaxValue or int.MinValue Arithmetic operations that result in overflow (in checked context) Casting between numeric types where the source value is too large for the target type ...

Read More

C# Decimal ("D") Format Specifier

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

The "D" (decimal) format specifier in C# is used to format integer types as a string of decimal digits (0-9). It displays the number with optional zero-padding to achieve a specified minimum length. Syntax Following is the syntax for the "D" format specifier − number.ToString("D") // Basic decimal format number.ToString("Dn") // Decimal with minimum n digits Where n is the minimum number of digits. If the number has fewer digits, it will be padded with leading zeros. Parameters ...

Read More

Represent Int64 as a String in C#

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

The Int64 data type in C# represents a 64-bit signed integer that can hold values from -9, 223, 372, 036, 854, 775, 808 to 9, 223, 372, 036, 854, 775, 807. Converting an Int64 to a string is a common operation that can be accomplished using several methods, with ToString() being the most straightforward approach. Syntax The basic syntax for converting an Int64 to string using ToString() method − long variable = value; string result = variable.ToString(); You can also use format specifiers with ToString() − string result = variable.ToString("format"); ...

Read More

Represent Int64 as a Hexadecimal String in C#

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

To represent Int64 as a hexadecimal string in C#, use the Convert.ToString() method and set the base as 16 for hexadecimal conversion. Int64 represents a 64-bit signed integer that can hold values from -9, 223, 372, 036, 854, 775, 808 to 9, 223, 372, 036, 854, 775, 807. Syntax Following is the syntax for converting Int64 to hexadecimal string − Convert.ToString(longValue, 16) You can also use the ToString() method with format specifiers − longValue.ToString("X") // Uppercase hex longValue.ToString("x") // Lowercase hex Using Convert.ToString() Method The ...

Read More

The "0" custom format specifier in C#

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

The "0" custom format specifier in C# is a zero placeholder that ensures a digit appears in each specified position. If the value being formatted has a digit in that position, it displays the digit; otherwise, it displays a zero. This format specifier is particularly useful for creating fixed-width numeric displays, padding numbers with leading zeros, or ensuring decimal places are always shown. Syntax Following is the syntax for using the "0" custom format specifier − number.ToString("000") // For integers with leading zeros number.ToString("0.00") // For decimals with ...

Read More

The "#" custom specifier in C#

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

The # custom format specifier in C# serves as a digit placeholder symbol in numeric formatting. It displays a digit only if one exists at that position, making it useful for creating flexible number formats that suppress unnecessary leading and trailing zeros. Syntax Following is the syntax for using the # format specifier − number.ToString("#formatPattern") String.Format("{0:#formatPattern}", number) How It Works When formatting a number, the # symbol represents an optional digit position. If the value has a digit at that position, it displays the digit. If not, nothing is displayed for that position. ...

Read More

C# Program to find a key in Dictionary

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

In C#, a Dictionary is a collection that stores key-value pairs. To check if a specific key exists in a Dictionary, you can use the ContainsKey() method, which returns true if the key is found and false otherwise. Syntax Following is the syntax for using the ContainsKey() method − bool result = dictionary.ContainsKey(key); Parameters key − The key to locate in the Dictionary. Return Value Returns true if the Dictionary contains an element with the specified key; otherwise, false. Using ContainsKey() Method The most straightforward way to ...

Read More

"." custom specifier in C#

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

The "." custom format specifier in C# adds a localized decimal separator into the output string. It determines the exact position where the decimal point will appear in the formatted numeric value. The first period in the format string determines the location of the decimal separator in the formatted value. Any additional periods are ignored and treated as literal characters. Syntax Following is the syntax for using the "." custom format specifier − number.ToString("format_with_decimal_point") String.Format("format_with_decimal_point", number) Where the format string contains a period (.) to specify decimal separator placement. How It Works ...

Read More
Showing 1591–1600 of 1,951 articles
« Prev 1 158 159 160 161 162 196 Next »
Advertisements