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 833 of 2109
C# Program to find the sum of a sequence
A sequence in C# is a collection of elements that can be processed using LINQ methods. To find the sum of a sequence, you can use several approaches including the Sum() method from LINQ, which provides a simple and efficient way to calculate the total. The most common approach is using the LINQ Sum() method, which can be applied to any IEnumerable collection. Syntax Following is the syntax for using the Sum() method − // For numeric collections collection.Sum(); // With selector function collection.Sum(x => x.Property); Using LINQ Sum() Method The ...
Read MoreC# Queryable Take() Method
The Take() method in C# is a LINQ extension method that returns a specified number of contiguous elements from the start of a sequence. It's commonly used with IQueryable collections to limit the number of results returned from a query. Syntax Following is the syntax for the Take() method − public static IQueryable Take( this IQueryable source, int count ) Parameters source − The IQueryable to return elements from. count − The number of elements to return. Return Value ...
Read MoreRepresent Int64 as a Octal string in C#
To represent Int64 as an octal string in C#, use the Convert.ToString() method and set the base as the second parameter to 8 for octal conversion. Int64 represents a 64-bit signed integer that can store 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 octal string − Convert.ToString(longValue, 8) Parameters longValue − The Int64 value to convert 8 − The base for octal number system Return Value Returns a string representation ...
Read MoreC# Program to order array elements
C# provides several methods to order array elements. The OrderBy() method sorts elements in ascending order, while ThenBy() is used for secondary sorting criteria when multiple elements have the same primary sort value. Syntax Following is the syntax for ordering array elements using LINQ methods − // Primary ordering IEnumerable result = array.OrderBy(item => item.Property); // Primary and secondary ordering IEnumerable result = array.OrderBy(item => item.Property1) ...
Read MoreC# Program to order array elements in descending order
To order array elements in descending order in C#, you can use various approaches including Array.Sort() with reversal, LINQ's OrderByDescending(), or custom comparison methods. These methods provide flexibility for sorting different data types and applying custom sorting criteria. Syntax Following is the syntax for basic descending sort using Array.Sort() and Array.Reverse() − Array.Sort(arrayName); Array.Reverse(arrayName); Following is the syntax for LINQ OrderByDescending() − var result = array.OrderByDescending(element => element); Using Array.Sort() and Array.Reverse() The simplest approach is to sort the array in ascending order first, then reverse it − ...
Read MoreC# Linq ThenBy Method
The ThenBy() method in C# LINQ is used to perform a secondary sort on a sequence that has already been ordered using OrderBy(). It allows you to sort by multiple criteria, where OrderBy() defines the primary sort and ThenBy() defines the secondary sort for elements that are equal in the primary sort. Syntax Following is the syntax for using ThenBy() method − IEnumerable result = source.OrderBy(keySelector1).ThenBy(keySelector2); You can chain multiple ThenBy() methods for additional sort levels − IEnumerable result = source.OrderBy(key1).ThenBy(key2).ThenBy(key3); Parameters keySelector − A function that ...
Read MoreC# Queryable Union Method
The Queryable Union method in C# performs a set union operation on two sequences, returning all unique elements from both sequences. It removes duplicate elements and preserves the order of first occurrence. Syntax Following is the syntax for the Queryable Union method − public static IQueryable Union( this IQueryable source1, IEnumerable source2 ) Parameters source1 − An IQueryable whose distinct elements form the first set for the union. source2 − An IEnumerable whose distinct elements form the second set for the union. ...
Read MoreC# Linq LastorDefault Method
The LastOrDefault() method in C# LINQ returns the last element of a sequence, or a default value if the sequence is empty. This method prevents exceptions that would occur with Last() when called on empty collections. Syntax Following is the syntax for LastOrDefault() method − public static T LastOrDefault(this IEnumerable source); public static T LastOrDefault(this IEnumerable source, Func predicate); Parameters source − The sequence to return the last element from. predicate − A function to test each element for a condition (optional). Return Value Returns the last element that ...
Read MoreC# Linq Zip Method
The Zip method in C# LINQ is used to merge two sequences element by element using a specified predicate function. It combines corresponding elements from two collections into a single sequence, creating pairs from the first element of each sequence, then the second element of each, and so on. The method stops when the shorter sequence is exhausted, making it safe to use with sequences of different lengths. Syntax Following is the syntax for the Zip method − public static IEnumerable Zip( this IEnumerable first, IEnumerable second, ...
Read MoreGet the width and height of a three-dimensional array
To get the width and height of a three-dimensional array in C#, we use the Array.GetLength() method. This method returns the number of elements in a specified dimension of the array. In a three-dimensional array declared as int[, , ] arr = new int[3, 4, 5], the dimensions represent different aspects of the array structure. Syntax Following is the syntax for getting dimensions of a three-dimensional array − arrayName.GetLength(dimension) Where dimension is − 0 − First dimension (rows) 1 − Second dimension (columns) 2 − Third dimension (depth) Understanding ...
Read More