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 858 of 2547
Understanding IndexOutOfRangeException Exception in C#
The IndexOutOfRangeException is a common runtime exception in C# that occurs when you attempt to access an array element using an index that is outside the valid range of indices for that array. This exception helps prevent memory corruption by catching invalid array access attempts. Arrays in C# are zero-indexed, meaning the first element is at index 0, and the last element is at index length - 1. Attempting to access an index less than 0 or greater than or equal to the array length will throw this exception. When IndexOutOfRangeException Occurs This exception is thrown in ...
Read MoreSorting a HashMap according to keys in C#
In C#, the equivalent of Java's HashMap is the Dictionary class, which stores key-value pairs. Sorting a Dictionary by keys requires extracting the keys, sorting them, and then accessing the values in the sorted order. Syntax Following is the syntax for creating and sorting a Dictionary by keys − Dictionary dict = new Dictionary(); // Sort keys using LINQ var sortedByKeys = dict.OrderBy(x => x.Key); // Or extract and sort keys manually var keys = dict.Keys.ToList(); keys.Sort(); Using LINQ OrderBy Method The most efficient way to sort a Dictionary by keys ...
Read MoreDoes declaring an array create an array in C#?
Declaring an array in C# does not create the actual array object in memory. Array declaration only creates a reference variable that can point to an array object. The array must be explicitly initialized using the new keyword to allocate memory and create the array instance. Array Declaration vs Array Creation Understanding the difference between declaration and creation is crucial for working with arrays in C# − Array Declaration vs Creation Declaration Only int[] arr; • Creates reference variable • No memory ...
Read MoreC# Program to access tuple elements
In C#, tuples are data structures that can hold multiple values of different types. Once you create a tuple, you can access its elements using the Item properties, where each element is numbered starting from Item1. Syntax Following is the syntax for creating a tuple using Tuple.Create() − var tupleName = Tuple.Create(value1, value2, value3, ...); Following is the syntax for accessing tuple elements − tupleName.Item1 // First element tupleName.Item2 // Second element tupleName.Item3 // Third element Using Item Properties to Access Elements Create a tuple with ...
Read MoreChained Exceptions in C#
Chained exceptions in C# allow you to preserve the original exception information while throwing a new exception. This creates a chain of exceptions where each exception wraps the previous one, maintaining the complete error context throughout the call stack. When an exception occurs deep in your application, chained exceptions help maintain the full error history by wrapping the original exception inside a new one using the innerException parameter of the Exception constructor. Syntax Following is the syntax for creating a chained exception − try { // code that might throw exception } ...
Read MoreTuple Rest Property in C#
The Rest property in C# tuples allows you to create tuples with eight or more elements by nesting additional tuple objects. When a tuple has more than seven elements, the eighth and subsequent elements are stored in the Rest property as a nested tuple. Syntax The general structure for an 8-element tuple using the Rest property is − Tuple Where TRest is another tuple type containing the remaining elements. You can create such tuples using Tuple.Create() − var myTuple = Tuple.Create(item1, item2, item3, item4, item5, item6, item7, item8); How the ...
Read MoreHow to call Math Operations using Delegates in C#?
To understand how to call Math Operations using Delegates in C#, let us see an example wherein we will perform mathematical operations like division, multiplication, and addition using delegates. A delegate in C# is a type that represents references to methods with a specific signature. Syntax Following is the syntax for declaring a delegate that can reference mathematical methods − delegate returnType DelegateName(parameters); Following is the syntax for assigning methods to a delegate − DelegateName delegateInstance = MethodName; // or DelegateName delegateInstance = new DelegateName(MethodName); Using Delegates for Basic Math ...
Read MoreHow to calculate the power exponent value using C#?
To calculate the power exponent value in C#, use the Math.Pow() method from the System namespace. This method raises a number to a specified power and returns the result as a double. Syntax Following is the syntax for the Math.Pow() method − public static double Pow(double x, double y) Parameters x − The base number (of type double). y − The exponent (of type double). Return Value Returns a double value representing x raised to the power of y (xy). Using Math.Pow() with Integers ...
Read MoreWhat is a managed code in C#?
Managed code in C# is code whose execution is managed by the Common Language Runtime (CLR). The CLR provides automatic memory management, type safety, exception handling, and garbage collection. When you write C# code, it gets compiled into Intermediate Language (IL) code, which is then executed by the CLR. Unlike unmanaged code (such as C/C++), managed code runs within the .NET runtime environment, which handles low-level operations automatically. This makes development safer and more efficient by reducing common programming errors like memory leaks and buffer overflows. How Managed Code Works The execution process of managed code follows ...
Read MoreInheritance vs Composition in C#
In C#, there are two fundamental ways to establish relationships between classes: inheritance and composition. Inheritance creates an "IS-A" relationship where a derived class inherits behavior from a base class, while composition creates a "HAS-A" relationship where one class contains instances of other classes as components. Inheritance Inheritance allows you to designate that a new class should inherit the members of an existing class. The existing class is called the base class, and the new class is referred to as the derived class. Inheritance implements the IS-A relationship. For example, mammal IS-A animal, dog IS-A mammal, hence dog ...
Read More