Csharp Articles

Page 97 of 196

Default value of StringBuilder in C#

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 627 Views

Use the default operator to get the default value of StringBuilder.StringBuilder str = default(StringBuilder);Above, we have used the default keywords to get the default value.Let us see the complete code −Exampleusing System; using System.Text; public class Demo {    public static void Main() {       StringBuilder str = default(StringBuilder);       Console.WriteLine("Default for StringBuilder = "+str);    } }OutputDefault for StringBuilder =The following is the output. It shows a blank space i.e. Null.Default for StringBuilder = Null

Read More

Default value of bool in C#

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

Use the default operator to get the default value of bool type −bool a = default(bool);Above, we have used the default keyword to get the default value.Let us see the code to display default value of bool −Exampleusing System; public class Demo {    public static void Main() {       bool a = default(bool);       // default for bool       Console.WriteLine("Default for bool type = "+a);    } }OutputThe following is the output. It shows a blank space i.e. False.Default for bool type = False

Read More

The nameof keyword in C#

George John
George John
Updated on 11-Mar-2026 602 Views

The nameof operator returns a string literal of an element that can be a variable, type or member.For example, the following is our variable −var vehicle = "motorbike";To get the string literal, use nameof −nameof(vehicle);The following is the code to implement nameof keyword −Exampleusing System; public class Program {    static void Main() {       var vehicle = "motorbike";       Console.WriteLine(nameof(vehicle));       var time = DateTime.Now.ToLocalTime();       Console.WriteLine(nameof(time));       var a = false;       Console.WriteLine(nameof(a));    } }Outputvehicle time a

Read More

Enum.GetName in C#

Ankith Reddy
Ankith Reddy
Updated on 11-Mar-2026 666 Views

Gets the string representation of an Enum value using the Enum.GetName.It has two parameters −Type − Enumeration typeObject − Value of an enumerationThe following is an example −Exampleusing System; class Demo {    enum Vehicle {       Car,       Motorbike,       Truck,       Bicycles    };    static void Main() {       // Usig GetName to get the string representation of enum type       string res = Enum.GetName(typeof(Vehicle), Vehicle.Motorbike);           // Displaying       Console.WriteLine(res);    } }OutputMotorbike

Read More

Enum.GetNames in C#

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 85 Views

It gets an array of the names of constants in an enumeration. The following is the syntax −Enum.GetNames(Type)Here, Type is an enumeration type.The following is an example −Exampleusing System; class Demo {    enum Vehicle {       Car,       Motorbike,       Truck,    };    static void Main() {       // display the enum       foreach ( string res in Enum.GetNames ( typeof (Vehicle)))       Console.WriteLine (res);    } }OutputCar Motorbike Truck

Read More

C# NullReferenceException

Chandu yadav
Chandu yadav
Updated on 11-Mar-2026 289 Views

NullReferenceException occurs when you try to to access member fields, or function types that points to null.Here is an example −Exampleusing System; class Demo {    static void Main() {       string str = null;       if (str.Length > 0) {          Console.WriteLine(str);       }    } }OutputThe following is the output. It throws NullReferenceException, since you are tryonhg access a memebt that points to null −Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Demo.Main () [0x00002] in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Demo.Main () [0x00002] in :0

Read More

Combine two arrays in C#

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

Firstly, declare and initialize two arrays −int[] arr1 = { 37, 45, 65 }; int[] arr2 = { 70, 89, 118 };Now create a new list −var myList = new List(); myList.AddRange(arr1); myList.AddRange(arr2);Use the AddRange() method the arrays into the newly created list.myList.AddRange(arr1); myList.AddRange(arr2);Now convert the list into array as shown below −Exampleusing System; using System.Collections.Generic; class Demo {    static void Main() {       int[] arr1 = { 37, 45, 65 };       int[] arr2 = { 70, 89, 118 };       // displaying array1       Console.WriteLine("Array 1...");       ...

Read More

Buffer Type in C#

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 776 Views

To handle range of bytes, use Buffer Type in C#. Its method Buffer.BlockCopy copies the bytes from one byte array to another byte array.Exampleusing System; class Demo {    static void Main() {       // byte arrays       byte[] b1 = new byte[] {39, 45, 58 };       byte[] b2 = new byte[5];       // copying bytes from one to another       Buffer.BlockCopy(b1, 0, b2, 0, 3);       /* calling the method with the byte array b2 that has the copied elements */       bufferFunc(b2);    }    static void bufferFunc(byte[] a) {       for (int j = 0; j < a.Length; j++) {          Console.Write(a[j]);       }       Console.WriteLine();    } }Output39455800

Read More

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

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

To replace a C# aray with a new array, use the Array.Resize.Withing that, set the size of the new array −Array.Resize(ref arr, 4);Now add the new elements to the array as shown below −Exampleusing System; class Program {    static void Main() {       char[] arr = new char[5];       arr[0] = 'J';       arr[1] = 'A';       Array.Resize(ref arr, 4);       // Set value for new elements       arr[2] = 'C';       arr[3] = 'K';       Console.WriteLine("Updated Array : "+ new string(arr));    } }OutputUpdated Array : JACK

Read More

C# program to count number of bytes in an array

Ankith Reddy
Ankith Reddy
Updated on 11-Mar-2026 1K+ Views

Set a byte array −byte[] b = { 5, 9, 19, 23, 29, 35, 55, 78 };To count number of bytes −Buffer.ByteLength(b)The following is the code −Exampleusing System; class Program {    static void Main() {       byte[] b = { 5, 9, 19, 23, 29, 35, 55, 78 };       int len = Buffer.ByteLength(b);       for (int i = 0; i < len; i++) {          Console.WriteLine(b[i]);       }       Console.WriteLine("Length of byte array = "+len);    } }Output5 9 19 23 29 35 55 78 Length of byte array = 8

Read More
Showing 961–970 of 1,951 articles
« Prev 1 95 96 97 98 99 196 Next »
Advertisements