Found 2587 Articles for Csharp

What is the difference between All and Any in C# Linq?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:30:58

2K+ Views

Any() method returns true if at least one of the elements in the source sequence matches the provided predicate. Otherwise, it returns false. On the other hand, the All() method returns true if every element in the source sequence matches the provided predicate. Otherwise, it returns falseExamplestatic void Main(string[] args){    IEnumerable doubles = new List { 1.2, 1.7, 2.5, 2.4 };    bool result = doubles.Any(val => val < 1);    System.Console.WriteLine(result);    IEnumerable doubles1 = new List { 0.8, 1.7, 2.5, 2.4 };    bool result1 = doubles1.Any(val => val < 1);    System.Console.WriteLine(result1);    Console.ReadLine(); }OutputFalse TrueExamplestatic ... Read More

What is dependency inversion principle and how to implement in C#?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:29:13

792 Views

High-level modules should not depend on low-level modules. Both should depend on abstractions.Abstractions should not depend on details. Details should depend on abstractions.This principle is primarily concerned with reducing dependencies among the code modules.ExampleCode Before Dependency Inversionusing System; namespace SolidPrinciples.Dependency.Invertion.Before{    public class Email{       public string ToAddress { get; set; }       public string Subject { get; set; }       public string Content { get; set; }       public void SendEmail(){          //Send email       }    }    public class SMS{       public ... Read More

What is proxy design pattern and how to implement it in C#?

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:19:51

281 Views

The Proxy pattern provides a surrogate or placeholder object to control access to another, different object.The Proxy object can be used in the same manner as its containing objectThe ParticipantsThe Subject defines a common interface for the RealSubject and the Proxy such that the Proxy can be used anywhere the RealSubject is expected.The RealSubject defines the concrete object which the Proxy represents.The Proxy maintains a reference to the RealSubject and controls access to it. It must implement the same interface as the RealSubject so that the two can be used interchangeablyProbably. If you've ever had a need to change the ... Read More

What is Interface segregation principle and how to implement it in C#?

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:16:41

358 Views

Clients should not be forced to depend upon interfaces that they don't use.The Interface Segregation Principle states that clients should not be forced to implement interfaces they don't use.Instead of one fat interface many small interfaces are preferred based on groups of methods, each one serving one submoduleBefore Interface SegregationExamplepublic interface IProduct {    int ID { get; set; }    double Weight { get; set; }    int Stock { get; set; }    int Inseam { get; set; }    int WaistSize { get; set; } } public class Jeans : IProduct {    public int ID { ... Read More

What is Facade and how to implement in C#?

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:14:49

172 Views

The Facade pattern is a simple structure laid over a more complex structure.The ParticipantsThe Subsystems are any classes or objects which implement functionality but can be "wrapped" or "covered" by the Facade to simplify an interface.The Facade is the layer of abstraction above the Subsystems, and knows which Subsystem to delegate appropriate work to.The Facade pattern is so general that it applies to almost every major app especially those where I couldn't refactor or modify pieces of said apps for various reasons.Examplepublic class HomeFacade {    LightsFacade light;    MusicSystemFacade musicSystem;    AcFacade ac;    public HomeFacade() {     ... Read More

What is Liskov Substitution principle and how to implement in C#?

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:12:32

330 Views

Derived types must be completely substitutable for their base types.Definition:We should be able to treat a child class as though it were the parent class. Essentially this means that all derived classes should retain the functionality of their parent class and cannot replace any functionality the parent provides.Before Liskov Substitutionpublic class Ellipse {    public double MajorAxis { get; set; }    public double MinorAxis { get; set; }    public virtual void SetMajorAxis(double majorAxis){       this.MajorAxis = majorAxis;    }    public virtual void SetMinorAxis(double minorAxis){       this.MajorAxis = minorAxis;    }    public ... Read More

Why singleton class is always sealed in C#?

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:08:03

2K+ Views

The sealed keyword means that the class cannot be inherited from. Declaring constructors private means that instances of the class cannot be created.You can have a base class with a private constructor, but still inherit from that base class, define some public constructors, and effectively instantiate that base class.Constructors are not inherited (so the derived class won't have all private constructors just because the base class does), and that derived classes always call the base class constructors first.Marking the class sealed prevents someone from trivially working around your carefully-constructed singleton class because it keeps someone from inheriting from the class.Examplestatic ... Read More

How to get all the files, sub files and their size inside a directory in C#?

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:05:22

1K+ Views

To get the files, C# provides a method Directory.GetFilesDirectory.GetFiles returns the names of all the files (including their paths) that match the specified search pattern, and optionally searches subdirectories.In the below example * is matches Zero or more characters in that position.SearchOption TopDirectoryOnly. Searches only the top directoriesSearchOption AllDirectories .Searches all the top directories and sub directoriesFileInfo gets the file information like Length, nameExample 1static void Main (string[] args) {    string rootPath = @"C:\Users\Koushik\Desktop\TestFolder";    var files = Directory.GetFiles(rootPath, "*.*", SearchOption.AllDirectories);    foreach (string file in files) {       Console.WriteLine(file);    }    Console.ReadLine (); }OutputC:\Users\Koushik\Desktop\TestFolder\TestFolderMain\TestFolderMain.txt ... Read More

How to implement IDisposable Design Pattern in C#?

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:03:45

2K+ Views

We should use an IDisposable design pattern (or Dispose Pattern) when we need to dispose of unmanaged objects.For implementing the IDisposable design pattern, the class which deals with unmanaged objects directly or indirectly should implement the IDisposable interface.And implement the method Dispose declared inside of the IDisposable interface. We do not directly deal with unmanaged objects. But we deal with managed classes, which deals directly with unmanaged objects. For example, File handlers, connection string, HTTP streams, etc.Important aspect of this pattern is that it makes easier for inherited classes to follow the IDisposable design pattern. And it is because of ... Read More

How to implement Null object Pattern in C#?

Nizamuddin Siddiqui
Updated on 25-Nov-2020 12:00:25

172 Views

The null object pattern helps us to write a clean code avoiding null checks where ever possible. Using the null object pattern, the callers do not have to care whether they have a null object or a real object. It is not possible to implement null object pattern in every scenario. Sometimes, it is likely to return a null reference and perform some null checks.Examplestatic class Program{    static void Main(string[] args){       Console.ReadLine();    }    public static IShape GetMobileByName(string mobileName){       IShape mobile = NullShape.Instance;       switch (mobileName){         ... Read More

Advertisements