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 828 of 2547
ToDictionary method in C#
The ToDictionary method is a LINQ extension method in C# that converts any collection into a Dictionary. It allows you to specify how to extract the key and value from each element in the source collection. Syntax Following is the basic syntax for the ToDictionary method − source.ToDictionary(keySelector) source.ToDictionary(keySelector, valueSelector) source.ToDictionary(keySelector, valueSelector, comparer) Parameters keySelector − A function to extract the key from each element. valueSelector − A function to extract the value from each element (optional). comparer − An equality comparer to compare keys (optional). ...
Read MoreC# SingleorDefault() Method
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 MoreC# Program to check if a number is prime or not
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. To check if a number is prime in C#, we need to verify that it has exactly two divisors. Algorithm To determine if a number is prime, we use a for loop to check all numbers from 1 to the given number. We count how many divisors exist by checking if the remainder is zero when dividing the number by each potential divisor − for (int i = 1; i
Read MoreHow to define custom methods in C#?
To define a custom method in C#, you create a reusable block of code that performs a specific task. Methods help organize your code, make it more readable, and avoid repetition by allowing you to call the same functionality multiple times. Syntax Following is the syntax for defining a custom method in C# − (Parameter List) { Method Body } Method Components The following are the various elements of a method − Access Specifier − This determines the visibility of a method from other classes. ...
Read MoreContainsValue in C#
The ContainsValue() method in C# is used to determine whether a Dictionary contains a specific value. It returns true if the value exists in the dictionary, and false otherwise. This method performs a sequential search through all values in the dictionary. Syntax Following is the syntax for using the ContainsValue() method − bool result = dictionary.ContainsValue(value); Parameters value − The value to search for in the dictionary. The type must match the value type of the dictionary. Return Value Returns true if the dictionary contains the specified value; otherwise, ...
Read MoreHow to sort a list of dictionaries by values of dictionaries in C#?
Sorting a list of dictionaries by their values is a common requirement in C# programming. There are multiple approaches to achieve this, ranging from sorting by keys to sorting by values, and even sorting lists containing multiple dictionaries. Syntax Following is the syntax for sorting dictionary entries by values using LINQ − var sortedByValue = dictionary.OrderBy(x => x.Value); var sortedByValueDesc = dictionary.OrderByDescending(x => x.Value); For sorting by keys − var sortedByKey = dictionary.OrderBy(x => x.Key); Sorting Dictionary by Keys The simplest approach is to sort dictionary entries by their ...
Read MoreInt16.GetTypeCode Method in C# with Examples
The Int16.GetTypeCode() method in C# is used to return the TypeCode for value type Int16. This method is part of the IConvertible interface and helps identify the underlying data type at runtime. The TypeCode enumeration provides a way to categorize the data types supported by the Common Language Runtime (CLR). For Int16 values, this method always returns TypeCode.Int16. Syntax Following is the syntax − public TypeCode GetTypeCode(); Return Value This method returns TypeCode.Int16, which is the enumerated constant representing the Int16 type. Using GetTypeCode() with Int16 Values Example Let ...
Read MoreC# program to check if a string contains any special character
To check if a string contains any special character in C#, you can use the Char.IsLetterOrDigit method. This method returns false for any character that is not a letter or digit, which means it's a special character. Special characters include symbols like @, #, $, %, &, punctuation marks, and whitespace characters that are not alphanumeric. Syntax Following is the syntax for checking if a character is a letter or digit − bool Char.IsLetterOrDigit(char c) To check for special characters, use the negation operator − if (!Char.IsLetterOrDigit(character)) { ...
Read MoreC# Linq Skip() Method
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 MoreWrite a C# program to find GCD and LCM?
In C#, finding the GCD (Greatest Common Divisor) and LCM (Least Common Multiple) of two numbers is a common mathematical programming task. The GCD is the largest positive integer that divides both numbers without remainder, while the LCM is the smallest positive integer that both numbers can divide evenly. These calculations are frequently used in mathematical applications, fraction simplification, and solving problems involving ratios and proportions. Mathematical Relationship There's an important mathematical relationship between GCD and LCM − GCD(a, b) × LCM(a, b) = a × b This means once we find the ...
Read More