Found 2587 Articles for Csharp

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

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

638 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 1 Live Democlass 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 2 Live Democlass 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

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

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:44:12

152 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 refersExample Live Democlass DeepCopy {    public int a = 10; } class Program {    static void Main() {       //Deep Copy       DeepCopy d = new DeepCopy();       d.a = 10;       DeepCopy d1 = new ... Read More

What does the [Flags] Enum Attribute mean in C#?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:41:57

625 Views

The Enum Flags is used to take an enumeration variable and allow it hold multiple values. It should be used whenever the enum represents a collection of flags, rather than representing a single valueUse the FlagsAttribute for an enumeration only if a bitwise operation (AND, OR, EXCLUSIVE OR) is to be performed on a numeric value.Define enumeration constants in powers of two, that is, 1, 2, 4, 8, and so on. This means the individual flags in combined enumeration constants do not overlap.Example Live Democlass Program {    [Flags]    enum SocialMediaFlags { None = 0, Facebook = 1, Twitter = ... Read More

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

549 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

1K+ 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

866 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

3K+ 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

3K+ 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

10K+ 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

Advertisements