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
Csharp Articles
Page 158 of 196
Represent 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 MoreGet bounds of a C# three-dimensional array
To get the bounds of a three-dimensional array in C#, use the GetUpperBound() and GetLowerBound() methods. These methods return the highest and lowest indices for a specified dimension of the array. The parameter passed to these methods specifies the dimension (0, 1, or 2 for a three-dimensional array). Understanding array bounds is crucial for safe array traversal and avoiding index out-of-range exceptions. Syntax Following is the syntax for getting array bounds − array.GetUpperBound(dimension) array.GetLowerBound(dimension) Parameters dimension: An integer specifying the dimension of the array (0-based indexing). Return ...
Read MoreArgumentNullException in C#
The ArgumentNullException is thrown when a null reference is passed to a method that does not accept it as a valid argument. This exception is part of the System namespace and helps prevent null reference errors by catching them early. This exception commonly occurs when methods expect non-null parameters but receive null values instead. It provides clear error messages indicating which parameter was null. Syntax Following is the syntax for throwing ArgumentNullException − throw new ArgumentNullException(paramName); throw new ArgumentNullException(paramName, "Custom message"); Following is the syntax for handling ArgumentNullException − try { ...
Read More