How to capture out of memory exception in C#?

Ankith Reddy
Updated on 17-Mar-2026 07:04:35

2K+ Views

The System.OutOfMemoryException occurs when the CLR fails to allocate sufficient memory for an operation. This exception is inherited from the System.SystemException class and can be caught using try-catch blocks to handle memory allocation failures gracefully. Common scenarios that trigger this exception include attempting to create very large arrays, working with StringBuilder objects that exceed their capacity, or processing extremely large datasets. Syntax Following is the syntax for catching OutOfMemoryException − try { // Code that might throw OutOfMemoryException } catch (OutOfMemoryException ex) { // Handle the exception ... Read More

C# program to return an array from methods

karthikeya Boyini
Updated on 17-Mar-2026 07:04:35

13K+ Views

In C#, methods can return arrays by specifying the array type in the method signature and using the return statement. This allows you to create, populate, and return arrays from methods to be used by the calling code. Syntax Following is the syntax for declaring a method that returns an array − dataType[] MethodName() { // create and populate array return arrayVariable; } To call the method and use the returned array − dataType[] result = MethodName(); Returning String Arrays from Methods ... Read More

Convert.ToDateTime Method in C#

Samual Sam
Updated on 17-Mar-2026 07:04:35

1K+ Views

The Convert.ToDateTime method in C# converts a string representation of a date and time to a DateTime object. This method is commonly used when you need to parse date strings from user input, files, or external data sources into a usable DateTime format. The method supports various date formats and provides automatic parsing based on the current culture settings of your system. Syntax Following are the most commonly used overloads of Convert.ToDateTime − public static DateTime ToDateTime(string value) public static DateTime ToDateTime(string value, IFormatProvider provider) public static DateTime ToDateTime(object value) Parameters ... Read More

How do you sort an array in C# in ascending order?

Ankith Reddy
Updated on 17-Mar-2026 07:04:35

10K+ Views

In C#, you can sort an array in ascending order using the built-in Array.Sort() method. This method modifies the original array directly and arranges elements from smallest to largest value. Syntax Following is the syntax for sorting an array in ascending order − Array.Sort(arrayName); Parameters arrayName − The array to be sorted. This parameter is required and the array is modified in-place. Using Array.Sort() Method The Array.Sort() method automatically sorts elements in ascending order. Here's how to sort an integer array − using System; ... Read More

How to use 'as' operator in C#?

Samual Sam
Updated on 17-Mar-2026 07:04:35

408 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
Updated on 17-Mar-2026 07:04:35

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
Updated on 17-Mar-2026 07:04:35

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
Updated on 17-Mar-2026 07:04:35

4K+ 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
Updated on 17-Mar-2026 07:04:35

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

Double.ToString Method in C#

AmitDiwan
Updated on 17-Mar-2026 07:04:35

269 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

Advertisements