Found 2628 Articles for Csharp

What is typeof, GetType or is in C#?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:39:46

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.Example Live Democlass 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) {         ... Read More

How to implement a Singleton design pattern in C#?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:36:24

444 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 the difference between Static class and Singleton instance in C#?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:32:54

860 Views

StaticStatic is a keywordStatic classes can contain only static membersStatic objects are stored in stack.Static cannot implement interfaces, inherit from other classesSingletonSingleton is a design patternSingleton is an object creational pattern with one instance of the classSingleton can implement interfaces, inherit from other classes and it aligns with the OOPS conceptsSingleton object can be passed as a referenceSingleton supports object disposalSingleton object is stored on heapSingleton objects can be clonedSingleton objects are stored in Heap

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

Nizamuddin Siddiqui
Updated on 05-Aug-2020 11:46:41

663 Views

C# interface members can be implemented explicitly or implicitly.Implicit implementations don't include the name of the interface being implemented before the member name, so the compiler infers this. The members will be exposed as public and will be accessible when the object is cast as the concrete type.The call of the method is also not different. Just create an object of the class and invoke it.Implicit interface cannot be used if there is same method name declared in multiple interfacesExampleinterface ICar {    void displayCar(); } interface IBike {    void displayBike(); } class ShowRoom : ICar, IBike {   ... Read More

How to use order by, group by in c#?

Nizamuddin Siddiqui
Updated on 05-Aug-2020 09:07:34

2K+ Views

Order by is used to sort the arrays in the ascending or in the descending orderGroupBy operator belong to Grouping Operators category. This operator takes a flat sequence of items, organize that sequence into groups (IGrouping) based on a specific key and return groups of sequenceExampleclass ElectronicGoods {    public int Id { get; set; }    public string Name { get; set; }    public string Category { get; set; }    public static List GetElectronicItems() {       return new List() {          new ElectronicGoods { Id = 1, Name = "Mobile", Category = ... Read More

How to subscribe to an Event in C# and can we have multiple subscribers to one Event in a C#?

Nizamuddin Siddiqui
Updated on 05-Aug-2020 08:54:19

2K+ Views

Events enable a class or object to notify other classes or objects when something of interest occurs.The class that raises the event is called the publisher and the classes that handle the event are called subscribers.In the EventAn event can have multiple subscribers. A subscriber can handle multiple events from multiple publishers.Events that have no subscribers are never raised.The publisher determines when an event is raised; the subscribers determine what action is taken in response to the event.Exampleclass Program {    static void Main() {       var video = new MP4() { Title = "Eminem" };     ... Read More

What is #if DEBUG and How to use it in C#?

Nizamuddin Siddiqui
Updated on 05-Aug-2020 08:49:08

7K+ Views

In Visual Studio Debug mode and Release mode are different configurations for building your .Net project.Select the Debug mode for debugging step by step their .Net project and select the Release mode for the final build of Assembly file (.dll or .exe).The Debug mode does not optimize the binary it produces because the relationship between source code and generated instructions is more complex.This allows breakpoints to be set accurately and allows a programmer to step through the code one line at a time.The Debug configuration of your program is compiled with full symbolic debug information which help the debugger figure ... Read More

What is bin and obj folder in C#?

Nizamuddin Siddiqui
Updated on 05-Aug-2020 08:46:35

1K+ Views

Whenever we write a C# code and build or run the solution it generates 2 folders −binobjThese bins and obj has the compiled codeWhy do we have 2 folders?The reason is the compilation process goes through 2 stepscompilinglinkingIn the compiling every individual file is compiled into individual unitsThese compiled files will be later linked into one unit which can be a dll or an exeWhatever happens in the compiled phase will be added into obj folderThe final compilation that is the linked phase will go into bin folderThis obj folder is used in Conditional compilation or incremental compilationEx − I ... Read More

What is the difference between IEnumerable and IQueryable in C#?

Nizamuddin Siddiqui
Updated on 05-Aug-2020 08:43:49

9K+ Views

IEnumerable exists in System.Collections Namespace.IQueryable exists in System. Linq Namespace.Both IEnumerable and IQueryable are forward collection.IEnumerable doesn’t support lazy loadingIQueryable support lazy loadingQuerying data from a database, IEnumerable execute a select query on the server side, load data in-memory on a client-side and then filter data.Querying data from a database, IQueryable execute the select query on the server side with all filters.IEnumerable Extension methods take functional objects.IQueryable Extension methods take expression objects means expression tree.ExampleIEnumerabledbContext dc = new dbContext (); IEnumerable list = dc.SocialMedias.Where(p => p.Name.StartsWith("T")); list = list.Take(1); Sql statement generated for the above querySELECT [t0].[ID], [t0].[Name] FROM ... Read More

What is Binary Serialization and Deserialization in C# and how to achieve Binary Serialization in C#?

Nizamuddin Siddiqui
Updated on 05-Aug-2020 08:35:45

2K+ Views

Converting an Object to a Binary format which is not in a human readable format is called Binary Serialization.Converting back the binary format to human readable format is called deserialization?To achieve binary serialization in C# we have to make use of library System.Runtime.Serialization.Formatters.Binary AssemblyCreate an object of BinaryFormatter class and make use of serialize method inside the classExampleSerialize an Object to Binary [Serializable] public class Demo {    public string ApplicationName { get; set; } = "Binary Serialize";    public int ApplicationId { get; set; } = 1001; } class Program {    static void Main()    {     ... Read More

Advertisements