karthikeya Boyini

karthikeya Boyini

1,421 Articles Published

Articles by karthikeya Boyini

Page 19 of 143

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

How to iterate over a C# dictionary?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 532 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

Round a number to the nearest even number in C#

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 847 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

How to list down all the files available in a directory using C#?

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

In C#, you can list all files in a directory using the DirectoryInfo class and its GetFiles() method. This approach provides detailed information about each file, including name, size, and other properties. Syntax Following is the syntax for creating a DirectoryInfo object and getting files − DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\path\to\directory"); FileInfo[] files = directoryInfo.GetFiles(); You can also specify search patterns and options − FileInfo[] files = directoryInfo.GetFiles("*.txt", SearchOption.TopDirectoryOnly); Using DirectoryInfo.GetFiles() The DirectoryInfo class provides detailed file information and is ideal when you need file properties like size, creation ...

Read More

Decimal constants in C#

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

The decimal type in C# provides built-in constants to retrieve the minimum and maximum possible values for decimal numbers. These constants are useful for validation, range checking, and initialization purposes. Syntax Following is the syntax for declaring a decimal constant − decimal variableName = value M; Following is the syntax for accessing decimal constants − decimal.MaxValue // Maximum possible decimal value decimal.MinValue // Minimum possible decimal value Decimal Constants The decimal type provides two important constants − decimal.MaxValue − Returns the largest possible decimal ...

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# Program to check if a number is Positive, Negative, Odd, Even, Zero

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

In C#, you can determine if a number is positive, negative, odd, even, or zero using simple conditional statements and arithmetic operations. This involves checking the sign of the number and using the modulus operator to determine if a number is divisible by 2. Logic for Number Classification To classify a number, we use the following approaches − Positive/Negative/Zero: Compare the number with zero using relational operators. Odd/Even: Use the modulus operator (%) to check if the remainder when divided by 2 is zero. Number Classification Logic ...

Read More

C# program to display factors of entered number

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

A factor of a number is any integer that divides the number evenly without leaving a remainder. For example, the factors of 12 are 1, 2, 3, 4, 6, and 12 because each of these numbers divides 12 exactly. To find factors of a number in C#, we use the modulus operator (%) to check if the remainder is zero when dividing the number by potential factors. Algorithm The algorithm to find factors involves the following steps − Start with 1 and iterate through all numbers up to the given number For each number, check ...

Read More

C# Program to display temporary file names

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

The Path.GetTempPath() method in C# retrieves the path of the system's temporary folder where temporary files are stored. This method is useful when your application needs to create temporary files or work with the system's designated temporary directory. Syntax Following is the syntax for using Path.GetTempPath() method − string tempPath = Path.GetTempPath(); Return Value The method returns a string representing the path to the system's temporary directory. On Windows, this is typically the path specified by the TEMP or TMP environment variables. On Unix-like systems, it's usually /tmp/. Using GetTempPath() to Display ...

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
Showing 181–190 of 1,421 articles
« Prev 1 17 18 19 20 21 143 Next »
Advertisements