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 99 of 2547
How to use 'as' operator in C#?
The as operator in C# performs safe type conversions between compatible reference types. Unlike direct casting, the as operator returns null if the conversion fails instead of throwing an exception, making it ideal for checking type compatibility. The as operator can perform reference conversions, nullable conversions, and boxing conversions, but cannot perform user-defined conversions or value type conversions (except nullable types). Syntax Following is the syntax for using the as operator − TargetType variable = expression as TargetType; If the conversion succeeds, variable contains the converted value. If it fails, variable is null. ...
Read MoreHow to capture index out of range exception in C#?
The IndexOutOfRangeException occurs when you try to access an array element with an index that is outside the bounds of the array. This is a common runtime exception in C# that can be captured using try-catch blocks. Understanding Array Bounds Arrays in C# are zero-indexed, meaning the first element is at index 0. If an array has 5 elements, the valid indices are 0, 1, 2, 3, and 4. Accessing index 5 or higher will throw an IndexOutOfRangeException. Array Index Bounds ...
Read MoreDeclare a const array in C#
In C#, you cannot directly declare a const array because arrays are reference types and const fields must be compile-time constants. However, you can achieve similar behavior using readonly fields or ReadOnlyCollection to create immutable arrays. Syntax Following is the syntax for declaring a readonly array − public static readonly DataType[] arrayName = { value1, value2, value3 }; Following is the syntax for using ReadOnlyCollection − public static readonly ReadOnlyCollection arrayName = new ReadOnlyCollection(new DataType[] { value1, value2, value3 }); Using readonly Static Arrays ...
Read MoreC# program to get the last element from an array
In C#, there are several ways to get the last element from an array. The most common approach is to use the array's Length property to access the element at the last index position. Syntax Following is the syntax to get the last element using array indexing − arrayName[arrayName.Length - 1] You can also use LINQ methods to get the last element − arrayName.Last() arrayName.LastOrDefault() Using Array Indexing The most efficient way to get the last element is by using the array's length minus one as the index − ...
Read MoreDifferent Star Pattern Programs in C#
Star pattern programs are common programming exercises that help developers practice loops and understand nested iteration logic. C# provides simple syntax to create various star patterns using for loops and Console.Write() methods. Common Star Pattern Types Right Triangle * ** Pyramid * *** Inverted *** ** Diamond * *** Right Triangle Pattern This pattern creates a right-angled triangle where each row contains one more star than the previous row − using System; class Program { static void Main() { for (int i = 1; i
Read MoreDouble.ToString Method in C#
The Double.ToString() method in C# is used to convert the numeric value of this instance to its equivalent string representation. This method provides several overloads to format the double value in different ways, including culture-specific formatting and custom format strings. Syntax Following is the syntax for the basic ToString() method − public override string ToString(); Following is the syntax for ToString() with format specifier − public string ToString(string format); Following is the syntax for ToString() with culture-specific formatting − public string ToString(IFormatProvider provider); Return Value ...
Read MoreHow to delete/remove an element from a C# array?
In C#, arrays have a fixed size once created, so you cannot truly delete an element. However, you can remove an element by shifting the remaining elements to fill the gap, or use collections like List for dynamic removal. Understanding Array Element Removal When removing an element from an array, the process involves shifting all elements after the target position one position to the left, effectively overwriting the element to be removed. Array Element Removal Process Original: 35 ...
Read MoreHow to find the size of a variable without using sizeof in C#?
In C#, the sizeof operator is commonly used to determine the size of a value type in bytes. However, there are alternative approaches to find the size of a variable without using sizeof, such as using the BitConverter class or Marshal.SizeOf method. Syntax Using sizeof operator − int size = sizeof(dataType); Using BitConverter.GetBytes() method − byte[] dataBytes = BitConverter.GetBytes(variable); int size = dataBytes.Length; Using Marshal.SizeOf() method − int size = Marshal.SizeOf(typeof(dataType)); Using BitConverter.GetBytes() Method The BitConverter.GetBytes() method converts a value to its byte array representation. ...
Read MoreC# program to iterate over a string array with for loop
A string array in C# can be iterated using a for loop to access each element by its index. The loop runs from index 0 to the length of the array minus one, allowing you to process each string element sequentially. Syntax Following is the syntax for iterating over a string array with a for loop − for (int i = 0; i < arrayName.Length; i++) { // Access element: arrayName[i] } The Length property returns the total number of elements in the array, and the loop variable i serves ...
Read MoreC# Program to merge sequences
In C#, you can merge sequences using the Zip method from LINQ. The Zip method combines elements from two sequences by pairing them together using a specified function. Syntax The Zip method syntax is − sequence1.Zip(sequence2, (first, second) => result) Parameters sequence1 − The first sequence to merge sequence2 − The second sequence to merge resultSelector − A function that defines how to combine elements from both sequences How It Works The Zip method pairs elements from two sequences by their index position. If sequences have different lengths, the ...
Read More