Monday, March 2, 2026

Creational Design Patterns

Creational patterns deal with object creation mechanisms.


1) Singleton Pattern

Ensures only one instance of a class exists.

Example

Used for logging, configuration, database connection, etc.

public class Logger
{
    private static Logger _instance;

    private Logger() { }

    public static Logger GetInstance()
    {
        if (_instance == null)
            _instance = new Logger();

        return _instance;
    }

    public void Log(string message)
    {
        Console.WriteLine(message);
    }
}

// Usage
Logger log1 = Logger.GetInstance();
Logger log2 = Logger.GetInstance();

Console.WriteLine(log1 == log2); // True (same instance)

2) Factory Method Pattern

Creates object without exposing instantiation logic.

Example

Creating different types of vehicles.

public interface IVehicle
{
    void Drive();
}

public class Car : IVehicle
{
    public void Drive()
    {
        Console.WriteLine("Car Driving");
    }
}

public class Bike : IVehicle
{
    public void Drive()
    {
        Console.WriteLine("Bike Riding");
    }
}

public class VehicleFactory
{
    public static IVehicle GetVehicle(string type)
    {
        if (type == "Car")
            return new Car();
        else if (type == "Bike")
            return new Bike();
        else
            return null;
    }
}

// Usage
IVehicle vehicle = VehicleFactory.GetVehicle("Car");
vehicle.Drive();

3) Abstract Factory Pattern

Creates families of related objects.

Example

Creating Windows and Mac UI elements.

public interface IButton
{
    void Render();
}

public class WindowsButton : IButton
{
    public void Render()
    {
        Console.WriteLine("Windows Button");
    }
}

public class MacButton : IButton
{
    public void Render()
    {
        Console.WriteLine("Mac Button");
    }
}

public interface IUIFactory
{
    IButton CreateButton();
}

public class WindowsFactory : IUIFactory
{
    public IButton CreateButton()
    {
        return new WindowsButton();
    }
}

public class MacFactory : IUIFactory
{
    public IButton CreateButton()
    {
        return new MacButton();
    }
}

// Usage
IUIFactory factory = new WindowsFactory();
IButton button = factory.CreateButton();
button.Render();

4) Builder Pattern

Builds complex object step by step.

Example

Building a House object.

public class House
{
    public string Walls { get; set; }
    public string Roof { get; set; }
}

public class HouseBuilder
{
    private House _house = new House();

    public HouseBuilder BuildWalls()
    {
        _house.Walls = "Concrete Walls";
        return this;
    }

    public HouseBuilder BuildRoof()
    {
        _house.Roof = "Steel Roof";
        return this;
    }

    public House GetHouse()
    {
        return _house;
    }
}

// Usage
House house = new HouseBuilder()
                .BuildWalls()
                .BuildRoof()
                .GetHouse();

5) Prototype Pattern

Creates new object by copying existing object.

Example

public class Person : ICloneable
{
    public string Name { get; set; }

    public object Clone()
    {
        return this.MemberwiseClone();
    }
}

// Usage
Person p1 = new Person { Name = "Naresh" };
Person p2 = (Person)p1.Clone();

Console.WriteLine(p2.Name);

Summary (Simple Understanding)

Singleton → One object only
Factory → Create object based on condition
Abstract Factory → Create related object families
Builder → Build complex object step by step
Prototype → Copy existing object

What is design pattern?

Design Pattern is a reusable solution to a common problem in software design.
It is not ready-made code, but a template or best practice to solve recurring design problems.
In simple words:
A design pattern tells how to structure classes and objects to solve a problem in a clean and maintainable way.

Types of Design Patterns (GoF – Gang of Four)
Design patterns are mainly divided into 3 categories:

1) Creational Patterns
Used to create objects in a proper and flexible way.
Singleton – Only one instance of a class is created.
Factory Method – Creates objects without specifying exact class.
Abstract Factory – Creates families of related objects.
Builder – Builds complex objects step by step.
Prototype – Creates new objects by copying existing object.

2) Structural Patterns
Used to organize classes and objects into larger structures.
Adapter – Converts one interface into another.
Bridge – Separates abstraction from implementation.
Composite – Treats individual objects and groups uniformly.
Decorator – Adds new behavior to objects dynamically.
Facade – Provides simple interface to complex system.
Flyweight – Reduces memory usage by sharing objects.
Proxy – Controls access to another object.

3) Behavioral Patterns
Used to manage communication between objects.
Observer – Notifies multiple objects when state changes.
Strategy – Defines family of algorithms and makes them interchangeable.
Command – Encapsulates a request as an object.
Chain of Responsibility – Passes request along a chain of handlers.
Mediator – Controls communication between objects.
State – Changes behavior based on internal state.
Template Method – Defines skeleton of algorithm.
Iterator – Access elements sequentially.
Memento – Saves and restores object state.
Visitor – Adds new operation without changing classes.
Interpreter – Defines grammar for a language.


Thursday, February 12, 2026

Static vs Non-Static Class Members in C#

 Static Members :Belong to the class, not to an object.
                            Only one copy exists.
                             Accessed using ClassName.
                            MemberNameMemory allocated only once.
public class Employee
{
    public static string CompanyName = "ABC Ltd";
}
Non-Static Members.
                            Belong to the object (instance).
                            Each object has its own copy.
                            Must create object to access.
public class Employee
{
    public string Name;
}

"Static members belong to the class and are shared by all objects. Non-static members belong to the object and require object creation to access."

Wednesday, February 11, 2026

What is a Class Member in C#

  class members are the variable and functions that are defined inside a class.

  anything written inside a class is called a class members

  types of class members

  Fields
  propertied
  methods
  constructors
  events
  indexers
  and  Nested class

  Ex : public class Employee
  {
  // Field
  private string name;

  // Property
  public string Name { get; set; }

  // Method
  public void Display()
  {
  Console.WriteLine("Employee Name");
  }

  // Constructor
  public Employee()
  {
  Console.WriteLine("Constructor Called");
  }
  }

  "Class members are the variables, properties, methods, and constructors defined inside a class. They represent the data and behavior of the class." 
  

 Fields  - Store data
 Properties - Control data access
 Methods - perform actions

 Constructor  - initialize object

Difference Between var and dynamic in C#

 Var: Var is implicitly typed
         The type is decided at compile time
         Once assigned, the type cannot change
         strongly typed. 
         intellisese worked properly.  
         Errors are checked at compile time

Ex:
var name = "Naresh";   // Compiler treats it as string
// name = 10; ❌ Error (cannot change type)


Dynamic: dynamice is resolved ar runtime
         type checking happens at runtime, not compile time
         the type can change
         no compile time type checking
         intellisense is limited.
         errors may occur at runtime
Ex: 
dynamic value = "Naresh";
value = 10;   // ✅ Allowed

value = true; // ✅ Allowed

Thursday, February 5, 2026

var vs dynamic

1.var
Type is decided at compile time
Strongly typed
Must be initialized at declaration
IntelliSense & compile-time checking available
Example
var x = 10; // int
var name = "Ram"; // string
👉 If you assign a wrong type later → compile-time error

2.dynamic
Type is decided at runtime
Not strongly typed
No compile-time checking
Errors occur at runtime
Example
dynamic x = 10;
x = "Ram"; // allowed
x.DoSomething(); // runtime error if method not found

3.Key Differences (Interview Points)

Feature

var

dynamic

Type resolution

Compile time

Runtime

Type safety

Yes

No

IntelliSense

Yes

No

Error checking

Compile time

Runtime

Type change allowed

No

Yes


4.When to use?
Use var when type is obvious and you want clean code
Use dynamic when working with:
COM objects
Reflection
JSON / loosely typed data

5.Important interview one-liner
var is statically typed, dynamic is dynamically typed.

abstract class

1. What is an Abstract Class?
An abstract class is a partially implemented class.
It can have 
It can have both abstract methods (no body) and normal methods (with body).

2. Key rule
An abstract class cannot be instantiated
It must be inherited by another class

3. What can an abstract class contain?
Abstract methods
Non-abstract (normal) methods
Fields
Properties
Constructors

4. Syntax example (interview-friendly)
abstract class Employee
{
public abstract void Work();

public void Login()
{
// common code
}
}

class Developer : Employee
{
public override void Work()
{
// implementation
}
}

5. Method implementation rule
Abstract methods must be overridden in the derived class
Normal methods are optional to override

6. Inheritance rule
Supports single inheritance only
Multiple inheritance is not allowed with classes

7. Abstract class vs Interface (short point)
Abstract class → Partial abstraction
Interface → Complete abstraction

8. Why do we use abstract classes?
To share common base functionality
To enforce base rules with flexibility
When some behavior is common and some is different

9. Important interview one-liner
An abstract class provides a base class with both implemented and unimplemented methods.

Interface - An interface defines what to do, not how to do it.

1. What is an Interface?
An interface is a contract.
It defines 
It defines what methods or properties a class must have, but not the implementation.

2. What can an interface contain?
Method declarations
Properties
Events
👉 No method body (before C# 8.0)

3. How do we use an interface?
A class implements an interface
The class must implement all methods of the interface

4. Syntax example (interview-friendly)
interface IEmployee
{
void Work();
}

class Developer : IEmployee
{
public void Work()
{
// implementation
}
}

5. Multiple inheritance support
C# does not support multiple inheritance with classes
But a class can implement multiple interfaces
class MyClass : IInterface1, IInterface2
{
}

6. Interface vs Abstract Class (short point)
Interface → 100% abstraction
Abstract class → Partial abstraction

7. Why do we use interfaces?
Loose coupling
Dependency Injection
Multiple inheritance
Better maintainability and testability

8. Important interview one-liner
An interface defines what to do, not how to do it.

How to Find Repeated Values in Excel Using COUNTIF

To identify repeated values in Excel,

after adding a new column for count,

after typing =COUNTIF(, come to the left arrow and move to the required column. After that, press Ctrl + Shift + End to go till the end. Then, put a comma. Next, press Ctrl + Shift + Up Arrow to come to the beginning of the column. After coming there, click Enter, then the result will come. If you drag the result, the output will come for all columns.

=COUNTIF(A2:A248,A2) - example 

Wednesday, January 7, 2026

How to Clear Visual Studio Cache || Visual Studio Component Cache Location

Visual Studio Component Cache Location
Main Path (User-specific)
%USERPROFILE%\AppData\Local\Microsoft\VisualStudio
Inside this folder, you will see version-specific folders like:
16.0_xxxxxx → Visual Studio 2019
17.0_xxxxxx → Visual Studio 2022

Common Cache Folders You Can Clear
ComponentModelCache (Most important)
%USERPROFILE%\AppData\Local\Microsoft\VisualStudio\16.0_xxxxxx\ComponentModelCache
%USERPROFILE%\AppData\Local\Microsoft\VisualStudio\17.0_xxxxxx\ComponentModelCache
Clearing this fixes:
Visual Studio slowness
IntelliSense issues
Extension load errors

MEF Cache
%USERPROFILE%\AppData\Local\Microsoft\VisualStudio\16.0_xxxxxx\Cache

Roslyn Cache (C# / IntelliSense)
%USERPROFILE%\AppData\Local\Microsoft\VisualStudio\17.0_xxxxxx\Roslyn

Designer Shadow Cache
%USERPROFILE%\AppData\Local\Microsoft\VisualStudio\Designer

How to Clear Visual Studio Cache (Safe Steps)
Method 1: Manual (Recommended)
Close Visual Studio completely
Press Win + R
Paste:
%USERPROFILE%\AppData\Local\Microsoft\VisualStudio
Open the required version folder (16.0 / 17.0)
Delete these folders:
ComponentModelCache
Cache
Roslyn (optional)
Restart Visual Studio
Cache will be recreated automatically

Method 2: Command Prompt
devenv /clearcache
Run from Developer Command Prompt for Visual Studio

What NOT to Delete
Do NOT delete:
Extensions folder
Settings files
.vs folder inside your solution (unless troubleshooting)