Csharp Articles

Page 6 of 196

What is the difference between List and IList in C#?

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

The main difference between List and IList in C# is that List is a class that represents a list of objects which can be accessed by index while IList is an interface that represents a collection of objects which can be accessed by index. The IList interface implemented from two interfaces and they are ICollection and IEnumerable.List and IList are used to denote a set of objects. They can store objects of integers, strings, etc. There are methods to insert, remove elements, search and sort elements of a List or IList. The major difference between List and IList is that ...

Read More

What is an Optional parameter in C#?

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

By default, all parameters of a method are required. A method that contains optional parameters does not force to pass arguments at calling time. It means we call method without passing the arguments.The optional parameter contains a default value in function definition. If we do not pass optional argument value at calling time, the default value is used.Thera are different ways to make a parameter optional.Using Default ValueExampleusing System; namespace DemoApplication{    class Demo{       static void Main(string[] args){          OptionalMethodWithDefaultValue(5);          //Value2 is not passed as it is optional     ...

Read More

How to update the value stored in a Dictionary in C#?

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

In C#, Dictionary is a generic collection which is generally used to store key/value pairs. In Dictionary, the key cannot be null, but value can be. A key must be unique. Duplicate keys are not allowed if we try to use duplicate key then compiler will throw an exception.As mentioned above a value in a dictionary can be updated by using its key as the key is unique for every value.myDictionary[myKey] = myNewValue;ExampleLet’s take a dictionary of students having id and name. Now if we want to change the name of the student having id 2 from "Mrk" to "Mark".using ...

Read More

What is the difference between Foreach and Parallel.Foreach in C#?

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

Foreach loop in C# runs upon a single thread and processing takes place sequentially one by one. Whereas Parallel.Foreach loop in C# runs upon multiple threads and processing takes place in a parallel way. Which means it is looping through all items at once without waiting for the previous item to complete.The execution of Parallel.Foreach is faster than normal ForEach. To use Parallel.ForEach loop we need to import System.Threading.Tasks namespace.Exampleusing System; using System.Collections.Generic; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; namespace DemoApplication{    class Demo{       static void Main(string[] args){          var animals = new List{ ...

Read More

How to create Guid value in C#?

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

A Globally Unique Identifier or GUID represents a gigantic identification number — a number so large that it is mathematically guaranteed to be unique not only in a single system like a database, but across multiple systems or distributed applications.The total number of unique keys (3.40282366×1038) is so large that the probability of the same number being generated twice is very small. For an application using 10 billion random GUIDs, the probability of a coincidence is approximately 1 in a quintillion.(1030)For example, in Retail domain if we want to generate a unique for each transaction so that a customer can ...

Read More

How to implement a Singleton design pattern in C#?

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

Singleton Pattern belongs to Creational type patternSingleton design pattern is used when we need to ensure that only one object of a particular class is Instantiated. That single instance created is responsible to coordinate actions across the application.As part of the Implementation guidelines we need to ensure that only one instance of the class exists by declaring all constructors of the class to be private. Also, to control the singleton access we need to provide a static property that returns a single instance of the object.ExampleSealed ensures the class being inherited and object instantiation is restricted in the derived classPrivate ...

Read More

What is typeof, GetType or is in C#?

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

Typeof()The type takes the Type and returns the Type of the argument.GetType()The GetType() method of array class in C# gets the Type of the current instance.isThe "is" keyword is used to check if an object can be casted to a specific type. The return type of the operation is Boolean.Exampleclass Demo { } class Program {    static void Main() {       var demo = new Demo();       Console.WriteLine($"typeof { typeof(Demo)}");       Type tp = demo.GetType();       Console.WriteLine($"GetType {tp}");       if (demo is Demo) {          System.Console.WriteLine($"is ...

Read More

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 690 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
Showing 51–60 of 1,951 articles
« Prev 1 4 5 6 7 8 196 Next »
Advertisements