Csharp Articles

Page 7 of 196

How do you do a deep copy of an object in .NET?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 201 Views

Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicatedDeep Copy is used to make a complete deep copy of the internal reference types.In another words a deep copy occurs when an object is copied along with the objects to which it refersExampleclass DeepCopy {    public int a = 10; } class Program {    static void Main() {       //Deep Copy       DeepCopy d = new DeepCopy();       d.a = 10;       DeepCopy d1 = new DeepCopy();       d1.a = d.a;       Console.WriteLine("{0} {1}", d1.a, d.a); // 10,10       d1.a = 5;       Console.WriteLine("{0} {1}", d1.a, d.a); //5,10       Console.ReadLine();    } }Output10 10 5 10

Read More

How to check if a number is a power of 2 in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 692 Views

A power of 2 is a number of the form 2n where n is an integerThe result of exponentiation with number two as the base and integer n as the exponent.n2n01122438416532Example 1class Program {    static void Main() {       Console.WriteLine(IsPowerOfTwo(9223372036854775809));       Console.WriteLine(IsPowerOfTwo(4));       Console.ReadLine();    }    static bool IsPowerOfTwo(ulong x) {       return x > 0 && (x & (x - 1)) == 0;    } }OutputFalse TrueExample 2class Program {    static void Main() {       Console.WriteLine(IsPowerOfTwo(9223372036854775809));       Console.WriteLine(IsPowerOfTwo(4));       Console.ReadLine();    }    static bool IsPowerOfTwo(ulong n) {       if (n == 0)          return false;       while (n != 1) {          if (n % 2 != 0)             return false;          n = n / 2;       }       return true;    } }OutputFalse True

Read More

How to convert C# DateTime to “YYYYMMDDHHMMSS” format?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 5K+ Views

Convert the dateTime to toString that results in converting the DateTime to “YYYYMMDDHHMMSS” formatThere are also other formats that the dateTime can be convertedMM/dd/yyyy 08/22/2020dddd, dd MMMM yyyy Tuesday, 22 August 2020dddd, dd MMMM yyyy HH:mm Tuesday, 22 August 2020 06:30dddd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2020 06:30 AMdddd, dd MMMM yyyy H:mm Tuesday, 22 August 2020 6:30dddd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2020 6:30 AMdddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2020 06:30:07MM/dd/yyyy HH:mm 08/22/2020 06:30MM/dd/yyyy hh:mm tt 08/22/2020 06:30 AMMM/dd/yyyy H:mm 08/22/2020 6:30MM/dd/yyyy h:mm tt 08/22/2020 6:30 AMMM/dd/yyyy HH:mm:ss 08/22/2020 06:30:07Exampleclass ...

Read More

What is the difference between Func delegate and Action delegate in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

A delegate is a type that represents references to methods with a particular parameter list and return type. When we instantiate a delegate, we can associate its instance with any method with a compatible signature and return type. We can invoke (or call) the method through the delegate instance.Func DelegateFunc is a generic delegate included in the System namespace. It has zero or more input parameters and one out parameter. The last parameter is considered as an out parameter. This delegate can point to a method that takes up to 16 Parameters and returns a value.Below is the Func delegate ...

Read More

How to convert byte array to an object stream in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

Stream is the abstract base class of all streams and it Provides a generic view of a sequence of bytes. The Streams Object involve three fundamental operations such as Reading, Writing and Seeking. A stream be can be reset which leads to performance improvements.A byte array can be converted to a memory stream using MemoryStream Class.MemoryStream stream = new MemoryStream(byteArray);ExampleLet us consider a byte array with 5 values 1, 2, 3, 4, 5.using System; using System.IO; namespace DemoApplication {    class Program {       static void Main(string[] args) {          byte[] byteArray = new byte[5] ...

Read More

How to create a comma separated string from a list of string in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 13K+ Views

A List of string can be converted to a comma separated string using built in string.Join extension method.string.Join(", " , list);This type of conversion is really useful when we collect a list of data (Ex: checkbox selected data) from the user and convert the same to a comma separated string and query the database to process further.Exampleusing System; using System.Collections.Generic; namespace DemoApplication {    public class Program {       static void Main(string[] args) {          List fruitsList = new List {             "banana",             "apple", ...

Read More

Why the error Collection was modified; enumeration operation may not execute occurs and how to handle it in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 21K+ Views

This error occurs when a looping process is being running on a collection (Ex: List) and the collection is modified (data added or removed) during the runtime.Exampleusing System; using System.Collections.Generic; namespace DemoApplication {    public class Program {       static void Main(string[] args) {          try {             var studentsList = new List {                new Student {                   Id = 1,                   Name = "John"                },                new Student {                   Id = 0,                   Name = "Jack"                },                new Student {                   Id = 2,                   Name = "Jack"                }             };             foreach (var student in studentsList) {                if (student.Id

Read More

How to convert byte array to string in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 20K+ Views

In .Net, every string has a character set and encoding. A character encoding tells the computer how to interpret raw zeroes and ones into real characters. It usually does this by pairing numbers with characters. Actually, it is the process of transforming a set of Unicode characters into a sequence of bytes.We can use Encoding.GetString Method (Byte[]) to decodes all the bytes in the specified byte array into a string. Several other decoding schemes are also available in Encoding class such as UTF8, Unicode, UTF32, ASCII etc. The Encoding class is available as part of System.Text namespace.string result = Encoding.Default.GetString(byteArray);Exampleusing ...

Read More

How to fetch a property value dynamically in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 7K+ Views

We can make use of Reflection to fetch a property value dynamically.Reflection provides objects (of type Type) that describe assemblies, modules and types. We can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If we use attributes in our code, reflection enables us to access them.The System.Reflection namespace and System.Type class play an important role in .NET Reflection. These two works together and allows us to reflect over many other aspects of a ...

Read More

What is the main difference between int.Parse() and Convert.ToInt32 in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 6K+ Views

Convert a string representation of number to an integer, using the int.Parse or Convert.ToInt32 method in C#. If the string cannot be converted, then the int.Parse or Convert.ToInt32 method returns an exceptionConvert.ToInt32 allows null value, it doesn't throw any errors Int.parse does not allow null value, and it throws an ArgumentNullException error.Exampleclass Program {    static void Main() {       int res;       string myStr = "5000";       res = int.Parse(myStr);       Console.WriteLine("Converting String is a numeric representation: " + res);       Console.ReadLine();    } }OutputConverting String is a numeric ...

Read More
Showing 61–70 of 1,951 articles
« Prev 1 5 6 7 8 9 196 Next »
Advertisements