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 39 of 2547
What is the use of "is" keyword in C#?
The is keyword in C# is used to check if an object is of a specific type or can be cast to that type. It returns a Boolean value — true if the object is compatible with the specified type, false otherwise. The is keyword is particularly useful for type checking before performing type conversions, ensuring safe downcasting in inheritance hierarchies, and working with polymorphic objects. Syntax Following is the syntax for using the is keyword − object is Type The is keyword can also be used with pattern matching (C# 7.0+) − ...
Read MoreHow to convert an integer to string with padding zero in C#?
Converting an integer to a string with padding zeros is a common requirement in C# programming. This allows you to format numbers with a consistent width by adding leading zeros. There are several approaches to achieve this, each with its own syntax and use cases. Syntax Following are the common syntaxes for zero-padding integers − // Using PadLeft method number.ToString().PadLeft(width, '0'); // Using custom numeric format number.ToString("0000"); // Using standard numeric format number.ToString("D4"); // Using string interpolation $"{number:0000}" Using PadLeft Method The PadLeft method pads the beginning of a string ...
Read MoreHow to catch an exception thrown by an async void method in C#?
In synchronous C# code, exceptions propagate up the call stack until they reach an appropriate catch block. However, exception handling in asynchronous methods behaves differently depending on the return type. An async method in C# can have three return types: void, Task, and Task. When an exception occurs in an async method with Task or Task return type, the exception is wrapped in an AggregateException and attached to the Task object. However, async void methods behave differently and present unique challenges for exception handling. Async Task vs Async Void Exception Handling Exception Handling: ...
Read MoreConsole.ResetColor() Method in C#
The Console.ResetColor() method in C# is used to reset the foreground and background console colors to their default values. This method is particularly useful when you have changed the console colors during program execution and want to restore the original appearance. Syntax Following is the syntax for the Console.ResetColor() method − public static void ResetColor(); Parameters This method takes no parameters. Return Value This method returns void (nothing). Using Console.ResetColor() Method The method restores both foreground and background colors to the system defaults. Here's how colors work in console ...
Read MoreQueue.ToArray Method in C#
The Queue.ToArray() method in C# copies all Queue elements to a new array. This method creates a shallow copy of the queue's elements in the same order (FIFO - First In, First Out). Syntax The syntax for the generic Queue is as follows − public T[] ToArray(); Return Value Returns a new array of type T[] containing copies of the elements from the Queue. The array elements maintain the same order as they would be dequeued from the queue. Queue.ToArray() Process Queue ...
Read MoreGetting an enumerator for a range of elements in the ArrayList in C#
To get an enumerator for a range of elements in the ArrayList, you can use the GetEnumerator(int, int) method. This method allows you to iterate through a specific subset of elements starting from a given index with a specified count. Syntax Following is the syntax for getting an enumerator for a range of elements − IEnumerator enumerator = arrayList.GetEnumerator(startIndex, count); Parameters startIndex − The zero-based starting index of the range. count − The number of elements in the range. Return Value Returns an IEnumerator object that can iterate through ...
Read MoreGet an enumerator that iterates through the SortedSet in C#
The SortedSet class in C# provides the GetEnumerator() method to retrieve an enumerator that iterates through the collection in sorted order. This enumerator allows you to manually control the iteration process using MoveNext() and Current properties. Syntax Following is the syntax for getting an enumerator from a SortedSet − SortedSet.Enumerator enumerator = sortedSet.GetEnumerator(); Following is the syntax for using the enumerator to iterate − while (enumerator.MoveNext()) { T currentElement = enumerator.Current; // process currentElement } Using GetEnumerator() with String Elements The following example ...
Read MoreHow to store n number of lists of different types in a single generic list in C#?
We can store n number of lists of different types in a single generic list by creating a list of List. This approach allows us to store lists containing integers, strings, or any other data type within a single container. Syntax Following is the syntax for creating a list that can hold multiple lists of different types − List containerList = new List(); Each inner list can store objects of any type − List innerList = new List(); innerList.Add(value); // value can be int, string, etc. containerList.Add(innerList); Using List of ...
Read MoreC# Program to Sort a List of String Names Using the LINQ OrderBy() Method
Sorting a list of string names is a common task in programming, and the LINQ OrderBy() method in C# provides an elegant and efficient way to accomplish this. In this article, we will explore how to sort string lists using LINQ, including both ascending and descending order sorting. What is LINQ OrderBy() Method? The LINQ OrderBy() method is used to sort elements of a sequence in ascending order based on a specified key. For descending order, you use OrderByDescending(). These methods work with any IEnumerable collection and return an IOrderedEnumerable. Syntax Following is the syntax for ...
Read MoreSystem.Reflection namespace in C#
The System.Reflection namespace in C# provides classes and interfaces that allow you to examine and interact with assemblies, modules, types, and members at runtime. This powerful feature enables reflection — the ability to inspect and manipulate code dynamically during execution. The Assembly class is central to this namespace, representing a loaded assembly in memory. You can access an assembly through a type's Assembly property or load it dynamically using various methods. Assembly Identity Components An assembly's identity consists of four key components − Simple name − the filename without the extension Version − from the ...
Read More