Found 2628 Articles for Csharp

How to create Guid value in C#?

Nizamuddin Siddiqui
Updated on 04-Aug-2020 07:31:57

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 update the value stored in a Dictionary in C#?

Nizamuddin Siddiqui
Updated on 04-Aug-2020 07:29:38

5K+ 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". Live ... Read More

What are named parameters in C#?

Nizamuddin Siddiqui
Updated on 04-Aug-2020 07:27:58

3K+ Views

Named parameters provides us the relaxation to remember or to look up the order of parameters in the parameter lists of called methods. The parameter for each argument can be specified by parameter name.NamedParameterFunction(firstName: "Hello", lastName: "World")Using named parameters in C#, we can put any parameter in any sequence as long as the name is there. The right parameter value based on their names will be mapped to the right variable. The parameters name must match with the method definition parameter names. Named arguments also improve the readability of our code by identifying what each argument represents.Example Live Demousing System; namespace ... Read More

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

Nizamuddin Siddiqui
Updated on 04-Aug-2020 07:26:05

1K+ 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.Example Live Demousing 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 ... Read More

How to make a method deprecated in C#?

Nizamuddin Siddiqui
Updated on 04-Aug-2020 07:23:31

4K+ Views

The Obsolete Attribute marks elements like classes, methods, properties, fields, delegates, and many others within our code as deprecated or obsolete. The attribute is read at compile time and it is used to generate a warning or an error to the developer.This attribute can help if we have ever wanted to make sure programmers use newer versions of methods. It also makes it easier when we are transitioning from older methods to newer ones. Marking an item as obsolete warns users that program elements will be removed in future versions of the code base.This attribute is found in the System ... Read More

What is an Optional parameter in C#?

Nizamuddin Siddiqui
Updated on 04-Aug-2020 07:20:32

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 ValueExample Live Demousing System; namespace DemoApplication{    class Demo{       static void Main(string[] args){          OptionalMethodWithDefaultValue(5);          //Value2 is not passed as it is optional   ... Read More

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

Nizamuddin Siddiqui
Updated on 04-Aug-2020 07:17:15

15K+ 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 the difference between Finalize and Dispose in C#?

Nizamuddin Siddiqui
Updated on 04-Aug-2020 07:15:02

3K+ Views

FinalizeFinalize() is called by the Garbage Collector before an object that is eligible for collection is reclaimed. Garbage collector will take the responsibility to deallocate the memory for the unreferenced object. The Garbage Collector calls this method at some point after there are no longer valid references to that object in memory.The framework does not guarantee that when this will happen, we can force for Garbage Collection but it will hurt performance of a program. Finalize() belongs to the Object class and it will be called by the runtime.Exampleusing System; namespace DemoApplication{    public class Demo{       ~Demo(){ ... Read More

What is explicit implementation and when to use in the interface in C#?

Nizamuddin Siddiqui
Updated on 04-Aug-2020 07:12:33

274 Views

If a class implements two interfaces that contain a member with the same signature, then implementing that member on the class will cause both interfaces to use that member as their implementation.It's possible to implement an interface member explicitly—creating a class member that is only called through the interface, and is specific to that interfaceExampleinterface ICar{    void display(); } interface IBike{    void display(); } class ShowRoom : ICar, IBike{    void ICar.display(){       throw new NotImplementedException();    }    void IBike.display(){       throw new NotImplementedException();    } } class Program{    static void Main(){       Console.ReadKey();    } }

What are union, intersect and except operators in Linq C#?

Nizamuddin Siddiqui
Updated on 04-Aug-2020 07:10:41

453 Views

UnionUnion combines multiple collections into a single collection and returns a resultant collection with unique elementsIntersectIntersect returns sequence elements which are common in both the input sequencesExceptExcept returns sequence elements from the first input sequence that are not present in the second input sequenceExampleclass Program{    static void Main(){       int[] count1 = { 1, 2, 3, 4 };       int[] count2 = { 2, 4, 7 };       var resultUnion = count1.Union(count2);       var resultIntersect = count1.Intersect(count2);       var resultExcept = count1.Except(count2);       System.Console.WriteLine("Union");       ... Read More

Advertisements