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
Programming Articles
Page 845 of 2547
How to iterate efficiently through an array of integers of unknown size in C#
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 MoreC# Program to search for a string in an array of strings
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 MoreInfinity or Exception in C# when divide by 0?
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 MoreHow to iterate over a C# dictionary?
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 MoreC# Linq Count method
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 MoreWhat does the @ prefix do on string literals in C#?
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 MoreWhat are tokens in C#?
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 MoreHow to join or concatenate two lists in C#?
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 MoreRound a number to the nearest even number in C#
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 MoreC# DefaultIfEmpty Method
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