Programming Articles

Page 845 of 2547

How to iterate efficiently through an array of integers of unknown size in C#

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

To iterate efficiently through an array of integers of unknown size in C#, there are several approaches available. The key is to use the array's Length property or built-in iteration methods that automatically handle the array size. Syntax Following is the syntax for basic array iteration using a for loop − for (int i = 0; i < arr.Length; i++) { // access arr[i] } Following is the syntax for using foreach loop − foreach (int element in arr) { // access element directly ...

Read More

C# Program to search for a string in an array of strings

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

Searching for a string in an array of strings is a common programming task. C# provides several methods to accomplish this, with the most straightforward being the LINQ Contains() method. This method returns a boolean value indicating whether the specified string exists in the array. Syntax Following is the syntax for using Contains() method to search in an array − array.Contains(searchString) You can also use it with AsQueryable() for LINQ queries − array.AsQueryable().Contains(searchString) Using LINQ Contains() Method The simplest approach is to use the LINQ Contains() method which returns ...

Read More

Infinity or Exception in C# when divide by 0?

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 2K+ Views

When dividing by zero in C#, the behavior depends on the data type being used. Integer division by zero throws a DivideByZeroException, while floating-point division by zero returns Infinity or NaN (Not a Number) without throwing an exception. Syntax Following is the basic division syntax that can result in divide-by-zero scenarios − result = dividend / divisor; Exception handling syntax for integer division − try { result = dividend / divisor; } catch (DivideByZeroException ex) { // handle exception } Integer Division ...

Read More

How to iterate over a C# dictionary?

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

A Dictionary in C# is a collection of key-value pairs that can be iterated using several different approaches. Each method provides access to keys, values, or both simultaneously. Syntax Following is the syntax for declaring and initializing a Dictionary − Dictionary dict = new Dictionary(); dict.Add(key, value); Following are the common iteration patterns − // Using KeyValuePair foreach (KeyValuePair pair in dict) { // Access pair.Key and pair.Value } // Using Keys collection foreach (TKey key in dict.Keys) { // Access key and dict[key] } ...

Read More

C# Linq Count method

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 749 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

What does the @ prefix do on string literals in C#?

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 841 Views

The @ prefix in C# creates a verbatim string literal, which means you don't need to escape special characters like backslashes, quotes, or newlines. This makes the string easier to read and write, especially for file paths, regular expressions, and multi-line text. Syntax Following is the syntax for verbatim string literals − @"string content here" To include a double quote inside a verbatim string, use two consecutive quotes − @"He said ""Hello"" to me" Using @ for File Paths The @ prefix eliminates the need to escape backslashes in ...

Read More

What are tokens in C#?

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

A token is the smallest element of a C# program that the compiler can recognize. Tokens are the building blocks of C# code and include keywords, identifiers, literals, operators, and punctuation marks. Understanding tokens is fundamental to writing valid C# programs. Types of Tokens in C# Keywords int, if, class Reserved words Identifiers myVar, Main Names Literals 42, "hello" Values Operators ...

Read More

How to join or concatenate two lists in C#?

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

In C#, there are multiple ways to join or concatenate two lists. The most common approaches include using the AddRange() method, LINQ's Concat() method, and the Union() method. Each method has different behaviors and use cases. Syntax Following is the syntax for using AddRange() to concatenate lists − list1.AddRange(list2); Following is the syntax for using LINQ Concat() method − var result = list1.Concat(list2).ToList(); Following is the syntax for using Union() to join lists without duplicates − var result = list1.Union(list2).ToList(); Using AddRange() Method The AddRange() ...

Read More

Round a number to the nearest even number in C#

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

The MidpointRounding.ToEven option is used with rounding methods in C# to round a number to the nearest even number when the fractional part is exactly 0.5. This is also known as banker's rounding or round half to even. Syntax Following is the syntax for rounding to the nearest even number − decimal.Round(value, digits, MidpointRounding.ToEven) Math.Round(value, digits, MidpointRounding.ToEven) Parameters value − The decimal or double number to be rounded digits − The number of decimal places in the return value MidpointRounding.ToEven − Rounds to the nearest even number ...

Read More

C# DefaultIfEmpty Method

Samual Sam
Samual Sam
Updated on 17-Mar-2026 345 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
Showing 8441–8450 of 25,466 articles
« Prev 1 843 844 845 846 847 2547 Next »
Advertisements