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
Server Side Programming Articles
Page 820 of 2109
Find missing number in a sequence in C#
Finding missing numbers in a sequence is a common programming problem. In C#, you can solve this efficiently using LINQ operations by comparing the original sequence with a complete range of numbers. Using LINQ Except Method The most straightforward approach is to create a complete range from the minimum to maximum value and use the Except method to find missing numbers − using System; using System.Collections.Generic; using System.Linq; public class Program { public static void Main() { List myList = new List(){1, 2, 3, 5, 8, ...
Read MoreIntersect two lists in C#
In C#, you can find common elements between two lists using the Intersect() method from LINQ. This method returns an IEnumerable containing elements that appear in both lists, automatically removing duplicates. Syntax Following is the syntax for using the Intersect() method − IEnumerable result = list1.Intersect(list2); The method can also accept a custom equality comparer − IEnumerable result = list1.Intersect(list2, comparer); Using Intersect() with Integer Lists The most common use case is finding intersection between two integer lists − using System; using System.Collections.Generic; using System.Linq; class ...
Read MoreConvert.ToInt16 Method in C#
The Convert.ToInt16 method in C# converts a specified value to a 16-bit signed integer (short). This method accepts various data types including double, string, boolean, and other numeric types, and returns a short value ranging from -32, 768 to 32, 767. When converting from floating-point numbers, the method performs rounding to the nearest integer. If the value is exactly halfway between two integers, it rounds to the even number. Syntax Following are the common overloads of the Convert.ToInt16 method − public static short ToInt16(double value); public static short ToInt16(string value); public static short ToInt16(bool value); ...
Read MoreConvert.ToUInt16 Method in C#
The Convert.ToUInt16 method in C# converts a specified value to a 16-bit unsigned integer (ushort). This method can convert various data types including strings, integers, doubles, and other numeric types to a ushort value ranging from 0 to 65, 535. Syntax Following are the common overloads of the Convert.ToUInt16 method − public static ushort ToUInt16(string value); public static ushort ToUInt16(int value); public static ushort ToUInt16(double value); public static ushort ToUInt16(bool value); Parameters value − The value to convert to a 16-bit unsigned integer. Can be a string representation of a number, numeric ...
Read MoreEnumerable.Repeat method in C#
The Enumerable.Repeat method is part of the System.Linq namespace and creates a sequence that contains the same element repeated a specified number of times. This method is useful for initializing collections or generating test data with repeated values. Syntax Following is the syntax for the Enumerable.Repeat method − public static IEnumerable Repeat(TResult element, int count) Parameters element − The value to be repeated in the resulting sequence. count − The number of times to repeat the element. Return Value Returns an IEnumerable that contains the ...
Read MoreC# Aggregate() method
The Aggregate() method in C# applies an accumulator function over a sequence of elements. It processes each element in the collection and combines them into a single result using a specified function. This method is part of LINQ and provides a powerful way to perform custom aggregation operations. Syntax The Aggregate() method has three main overloads − // Simple aggregation without seed TSource Aggregate(Func func) // Aggregation with seed value TAccumulate Aggregate(TAccumulate seed, Func func) // Aggregation with seed and result selector TResult Aggregate(TAccumulate seed, Func func, Func resultSelector) Parameters ...
Read MoreRepresent Int64 as a Binary string in C#
To represent Int64 as a binary string in C#, use the Convert.ToString() method with base 2 as the second parameter. 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 binary string − Convert.ToString(longValue, 2) Parameters longValue − The Int64 value to convert 2 − The base for binary representation Return Value Returns a string representation of the Int64 value in binary format (base ...
Read MoreC# All method
The All() method in C# is a LINQ extension method that checks whether all elements in a collection satisfy a given condition. It returns a bool value − true if all elements meet the condition, or false if even one element fails the condition. The method is available for any IEnumerable collection and is commonly used with arrays, lists, and other collections for validation and filtering operations. Syntax Following is the syntax for the All() method − bool result = collection.All(predicate); Parameters predicate − A lambda expression or delegate that ...
Read MoreC# Linq Distinct() Method
The Distinct() method in C# LINQ is used to remove duplicate elements from a collection and return only unique elements. This method is available for any collection that implements IEnumerable and preserves the order of first occurrence. Syntax Following is the basic syntax for the Distinct() method − IEnumerable Distinct() IEnumerable Distinct(IEqualityComparer comparer) Parameters comparer (optional) − An IEqualityComparer to compare elements for equality. If not provided, the default equality comparer is used. Return Value Returns an IEnumerable containing distinct elements from the source sequence. Using ...
Read MoreC# Queryable LongCount Method
The LongCount() method in C# is part of the System.Linq namespace and returns the number of elements in a sequence as a long data type. This method is particularly useful when dealing with large collections where the count might exceed the range of an int (2, 147, 483, 647). The LongCount() x % 2 == 0); Console.WriteLine("Even numbers: {0}", evenCount); long greaterThanFive = numbers.AsQueryable().LongCount(x => x > 5); ...
Read More