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
Server Side Programming Articles
Page 809 of 2109
Default value of StringBuilder in C#
The default operator in C# returns the default value for any data type. For reference types like StringBuilder, the default value is null. This is useful when you need to initialize a StringBuilder variable without creating an instance immediately. Syntax Following is the syntax for using the default operator with StringBuilder − StringBuilder variable = default(StringBuilder); In C# 7.1 and later, you can also use the simplified syntax − StringBuilder variable = default; Using default(StringBuilder) Example using System; using System.Text; public class Demo { ...
Read MoreDefault value of bool in C#
The default operator in C# returns the default value for any data type. For the bool type, the default value is false. When a bool variable is declared without initialization, it automatically gets assigned the default value of false. Syntax Following is the syntax for using the default operator with bool − bool variable = default(bool); In C# 7.1 and later, you can also use the simplified syntax − bool variable = default; Using default(bool) Operator The following example demonstrates how to get the default value of bool ...
Read MoreThe nameof keyword in C#
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 MoreEnum.GetName in C#
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 MoreEnum.GetNames in C#
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 MoreC# NullReferenceException
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 MoreCombine two arrays in C#
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 MoreBuffer Type in C#
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 MoreReplace a C# array with a new array of different size
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 MoreC# program to count number of bytes in an array
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