C# Object and Class:
C# allows you to define classes, which are blueprints for creating objects. An object is an instance of a class.Example:
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Person person1 = new Person();
person1.Name = "John";
person1.Age = 30;
C# Constructor:
Constructors are special methods in a class that initializes its objects. They have the same name as the class and can have parameters.Example:
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
Person person1 = new Person("Alice", 25);
C# Destructor:
A destructor is used to release resources when an object is about to be destroyed. It’s rarely used in C# because the garbage collector handles memory management.Example:
class MyClass
{
~MyClass()
{
// Cleanup code here
}
}
C# this:
‘this’ refers to the current instance of a class and can be used to access its members.Example:
class Person
{
public string Name { get; set; }
public void SetName(string name)
{
this.Name = name;
}
}
C# static:
‘static’ members belong to the class itself, not instances. They are shared among all objects of the class.Example:
class MathUtility
{
public static int Add(int a, int b)
{
return a + b;
}
}
int result = MathUtility.Add(5, 3);
C# static constructor:
A static constructor is used to initialize static members of a class. It’s called once when the class is first accessed.Example:
class MyClass
{
public static int MyStaticProperty { get; set; }
static MyClass()
{
MyStaticProperty = 42;
}
}
C# Structs:
Structs are value types that can contain data members but not methods. They are often used for lightweight objects.Example:
struct Point
{
public int X;
public int Y;
}
Point p1 = new Point { X = 2, Y = 3 };
C# Enum:
Enums are used to define a set of named constant values. They provide better code readability.Example:
enum DaysOfWeek
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
DaysOfWeek today = DaysOfWeek.Wednesday;
Basic Inheritance:
Inheritance in C# allows a class to inherit properties and methods from another class.Example:
class Animal
{
public string Name { get; set; }
public void Eat()
{
Console.WriteLine($"{Name} is eating.");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine($"{Name} is barking.");
}
}
Method Overriding in Inheritance:
C# allows you to override methods in derived classes to provide specialized implementations.Example:
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a shape");
}
}
class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a circle");
}
}
Abstract Classes and Inheritance:
Abstract classes in C# are used to provide a base for other classes but cannot be instantiated themselves.Example:
abstract class Vehicle
{
public abstract void Start();
}
class Car : Vehicle
{
public override void Start()
{
Console.WriteLine("Car started");
}
}
C# Member Overloading:
Member overloading in C# allows you to define multiple methods or constructors with the same name in a class, differing in the number or type of their parameters. This enables flexibility in how you can interact with objects of the class.Example:
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
}
C# Method Overriding:
Method overriding is a concept in object-oriented programming where a derived class provides a specific implementation for a method defined in its base class. It allows you to customize the behavior of inherited methods.Example:
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Animal makes a generic sound.");
}
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Dog barks!");
}
}
C# Base:
The ‘base’ keyword in C# is used to call a method or constructor of the base class from within a derived class. It is often used when overriding methods to ensure that the base class behavior is preserved or extended.Example:
public class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a shape");
}
}
public class Circle : Shape
{
public override void Draw()
{
base.Draw(); // Calling the base class method
Console.WriteLine("Drawing a circle");
}
}
C# Polymorphism:
Polymorphism is a fundamental concept in C# and object-oriented programming, which allows objects of different classes to be treated as objects of a common base class. It facilitates code reusability and flexibility.Example:
Shape[] shapes = new Shape[]
{
new Circle(),
new Rectangle(),
new Triangle()
};
foreach (Shape shape in shapes)
{
shape.Draw(); // Polymorphic method call
}
C# Sealed:
The ‘sealed’ keyword in C# is used to prevent a class from being inherited or a method from being overridden by derived classes. It's useful when you want to restrict further modification of a class or method.Example:
public sealed class FinalClass
{
// Class members
}
public class DerivedClass // This will result in a compilation error
{
// Attempting to inherit from a sealed class
}