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
Articles by Samual Sam
Page 22 of 151
ElementAt() method in C#
The ElementAt() method in C# is a LINQ extension method that retrieves an element at a specified index from any collection that implements IEnumerable. It provides a way to access elements by index in collections that don't have built-in indexing support. Syntax Following is the syntax for the ElementAt() method − public static TSource ElementAt(this IEnumerable source, int index) Parameters source − The IEnumerable to return an element from. index − The zero-based index of the element to retrieve. Return Value Returns the element at the specified position in ...
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 MoreWhat are static or fixed length arrays in C#?
A static array or fixed-length array in C# is a data structure with a predetermined size that cannot be changed after creation. Once you declare an array with a specific length, that size remains constant throughout the program's execution. Static arrays are different from dynamic collections like List because their size is immutable. This makes them memory-efficient and provides predictable performance characteristics. Syntax Following is the syntax for declaring a static array − dataType[] arrayName = new dataType[size]; You can also initialize a static array with values at declaration − dataType[] ...
Read MoreWhat is the difference between Write() and WriteLine() methods in C#?
The difference between Write() and WriteLine() methods in C# is based on the new line character. Both methods are used to display output to the console, but they handle line termination differently. Write() method displays the output but does not provide a new line character, so subsequent output appears on the same line. WriteLine() method displays the output and automatically adds a new line character at the end, moving the cursor to the next line for subsequent output. Syntax Following is the syntax for Write() method − Console.Write(value); Console.Write("text"); Following is the syntax ...
Read MoreSkipWhile method in C#
The SkipWhile method in C# is a LINQ extension method that skips elements from the beginning of a sequence as long as a specified condition is true. It stops skipping once it encounters the first element that doesn't meet the condition and returns all remaining elements, including any subsequent elements that might match the condition again. This method is particularly useful when you need to skip a consecutive sequence of elements from the start of a collection based on a specific criteria. Syntax Following is the syntax for the SkipWhile method − public static IEnumerable ...
Read MoreHow to capture divide by zero exception in C#?
The System.DivideByZeroException is a class that handles errors generated from dividing a number by zero. This exception occurs when you attempt to divide an integer or decimal value by zero during arithmetic operations. In C#, you can capture and handle this exception using try-catch blocks to prevent your application from crashing and provide meaningful error messages to users. Syntax Following is the syntax for handling divide by zero exception − try { result = dividend / divisor; } catch (DivideByZeroException e) { // Handle the exception ...
Read MoreC# Program to get the first three elements from a list
The Take() method in C# is a LINQ extension method used to retrieve a specified number of elements from the beginning of a collection. It returns an IEnumerable containing the first n elements from the original collection. Syntax Following is the syntax for using the Take() method − IEnumerable Take(int count) Parameters count − An integer specifying the number of elements to return from the start of the sequence. Return Value The Take() method returns an IEnumerable that contains the specified number of elements from the start ...
Read MoreTypeof() vs GetType() in C#
In C#, both typeof() and GetType() are used to obtain type information, but they work differently. typeof() is a compile-time operator that returns the Type object for a specified type, while GetType() is a runtime method that returns the actual type of an object instance. Syntax Following is the syntax for using typeof() operator − Type type = typeof(TypeName); Following is the syntax for using GetType() method − Type type = objectInstance.GetType(); Using typeof() Operator The typeof() operator takes a type name as its argument and returns the Type ...
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 MoreThe Object Class in C#
The Object class is the base class of all classes in C# and the .NET Framework. Every class in C# implicitly inherits from the Object class, which means all objects have access to the fundamental methods provided by this base class. When you create any class in C#, it automatically inherits from Object even if you don't explicitly specify it. This provides a common set of methods that every object can use. Syntax Every class implicitly inherits from Object − public class MyClass { // This class inherits from Object automatically } ...
Read More