Csharp Articles

Page 134 of 196

The nameof keyword in C#

George John
George John
Updated on 17-Mar-2026 613 Views

The nameof operator in C# returns the string literal name of a variable, type, or member. It provides a compile-time constant string that represents the name of the code element, making it useful for logging, exception messages, and property change notifications without hardcoding strings. Syntax Following is the syntax for using the nameof operator − string name = nameof(element); Where element can be a variable, property, method, class, or namespace. Using nameof with Variables The nameof operator returns the variable name as a string, which is resolved at compile time − ...

Read More

Enum.GetName in C#

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 694 Views

The Enum.GetName method in C# returns the string name of a constant in a specified enumeration that has the specified value. This method is useful when you need to convert an enum value back to its string representation for display purposes or logging. Syntax Following is the syntax for the Enum.GetName method − public static string GetName(Type enumType, object value) Parameters enumType − The enumeration type whose constant's string name you want to retrieve. value − The value of a particular enumerated constant in terms of its underlying type. ...

Read More

Enum.GetNames in C#

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 103 Views

The Enum.GetNames() method in C# retrieves an array of the names of constants in a specified enumeration. This method is useful when you need to dynamically access or display all the values defined in an enum without hardcoding them. Syntax Following is the syntax for the Enum.GetNames() method − public static string[] GetNames(Type enumType) Parameters enumType − The System.Type of the enumeration whose names you want to retrieve. Return Value Returns a string[] array containing the names of the constants in the specified enumeration, ordered by their ...

Read More

C# NullReferenceException

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 302 Views

A NullReferenceException is one of the most common runtime exceptions in C#. It occurs when you try to access a member (property, method, or field) of an object reference that is null. When NullReferenceException Occurs This exception is thrown when − You call a method on a null reference You access or modify a property of a null reference You get the length of a null array or string You index into a null array NullReferenceException Flow ...

Read More

Combine two arrays in C#

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

Combining two arrays in C# can be accomplished using various methods. The most common approaches include using List.AddRange() method, Array.Copy(), or LINQ's Concat() method. Syntax Using List.AddRange() method − var list = new List(); list.AddRange(array1); list.AddRange(array2); T[] combinedArray = list.ToArray(); Using Array.Copy() method − T[] combinedArray = new T[array1.Length + array2.Length]; Array.Copy(array1, 0, combinedArray, 0, array1.Length); Array.Copy(array2, 0, combinedArray, array1.Length, array2.Length); Using LINQ Concat() method − T[] combinedArray = array1.Concat(array2).ToArray(); Using List.AddRange() Method This approach creates a List, adds both arrays using AddRange(), then converts ...

Read More

Buffer Type in C#

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 794 Views

The Buffer class in C# provides efficient methods for manipulating arrays of primitive types at the byte level. It is particularly useful when you need to perform fast operations on large arrays or when working with binary data. The Buffer.BlockCopy method is the most commonly used method, which copies bytes from one array to another. Syntax Following is the syntax for the Buffer.BlockCopy method − Buffer.BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count); Parameters src − The source array from which bytes are copied srcOffset − The byte offset into ...

Read More

Replace a C# array with a new array of different size

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

To replace a C# array with a new array of different size, use the Array.Resize method. This method allows you to change the size of an existing array, either expanding or shrinking it while preserving existing elements. Syntax Following is the syntax for using Array.Resize − Array.Resize(ref array, newSize); Parameters array − The array to resize (passed by reference using ref) newSize − The new size of the array How It Works When you resize an array: If the new size is ...

Read More

C# program to count number of bytes in an array

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

In C#, you can count the number of bytes in an array using the Buffer.ByteLength() method. This method returns the total number of bytes occupied by all elements in the array, which is particularly useful when working with different data types that have varying byte sizes. Syntax Following is the syntax for using Buffer.ByteLength() − int byteCount = Buffer.ByteLength(array); Parameters array − The array whose byte length you want to determine. It must be an array of primitive types. Return Value The method returns an int representing ...

Read More

Merge two arrays using C# AddRange() method

Samual Sam
Samual Sam
Updated on 17-Mar-2026 3K+ Views

The AddRange() method in C# is used to add elements from a collection to the end of a List. This method is particularly useful for merging arrays by first converting them to a list, adding both arrays using AddRange(), and then converting the result back to an array. Syntax Following is the syntax for the AddRange() method − public void AddRange(IEnumerable collection) Parameters collection − The collection whose elements should be added to the end of the List. The collection itself cannot be null, but it can contain elements that are null. ...

Read More

C# program to copy a range of bytes from one array to another

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

The Buffer.BlockCopy method in C# is used to copy a range of bytes from one array to another. This method provides a fast, low-level approach for copying data between arrays and is particularly useful when working with byte arrays or when you need to copy a specific range of elements. Syntax Following is the syntax for the Buffer.BlockCopy method − Buffer.BlockCopy(sourceArray, sourceOffset, destinationArray, destinationOffset, count); Parameters sourceArray − The source array from which to copy bytes sourceOffset − The zero-based byte offset into the source array destinationArray − The destination array to ...

Read More
Showing 1331–1340 of 1,951 articles
« Prev 1 132 133 134 135 136 196 Next »
Advertisements