Programming Articles

Page 99 of 2547

How to use 'as' operator in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 393 Views

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 More

How to capture index out of range exception in C#?

George John
George John
Updated on 17-Mar-2026 2K+ Views

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 More

Declare a const array in C#

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 13K+ Views

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 More

C# program to get the last element from an array

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 3K+ Views

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 More

Different Star Pattern Programs in C#

George John
George John
Updated on 17-Mar-2026 27K+ Views

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 More

Double.ToString Method in C#

AmitDiwan
AmitDiwan
Updated on 17-Mar-2026 241 Views

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 More

How to delete/remove an element from a C# array?

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 11K+ Views

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 More

How to find the size of a variable without using sizeof in C#?

Syed Javed
Syed Javed
Updated on 17-Mar-2026 2K+ Views

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 More

C# program to iterate over a string array with for loop

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 3K+ Views

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 More

C# Program to merge sequences

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 246 Views

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
Showing 981–990 of 25,467 articles
« Prev 1 97 98 99 100 101 2547 Next »
Advertisements