20. object
An object in C# is a runtime instance of a class, containing its own unique state and behavior. When a class is defined,It acts as a template. An object is a specific version of that template stored in memory.. Each object has its own set of values for the properties defined in its class.
For example, given a Person class:
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public void Introduce()
{
Console.WriteLine($”Hi, I’m {Name} and I’m {Age} years old.”);
}
}
We can create multiple independent objects of this class:
Person person1 = new Person { Name = “Alice”, Age = 30 };
Person person2 = new Person { Name = “Bob”, Age = 25 };
person1.Introduce(); // Output: Hi, I’m Alice and I’m 30 years old.
person2.Introduce(); // Output: Hi, I’m Bob and I’m 25 years old.
Each object keeps its own copy of data. This allows applications to behave dynamically. The .NET garbage collector manages objects in memory. It automatically removes objects that are no longer used. Understanding objects is important for using OOP principles. These principles include encapsulation, inheritance, and polymorphism in C#.
21. field
A field is a variable. It is declared inside a class or struct. A field represents the internal state or data of an object. Fields are used to store information relevant to an object, such as an entity’s name, age, or status. Unlike properties, fields provide direct storage without encapsulation through accessors.
Example:
class Person
{
public string name; // Public field
private int age; // Private field
}
Fields can have different access modifiers (public, private, protected) that control their visibility. Fields can be used freely inside a class. However, it is best practice to use private fields. This helps control data access through properties.
22. property
A property is a member that wraps a field. It provides methods to get and set the value. This ensures that access to the data is controlled. Properties are different from fields. They let you validate and change values. They also set rules on how values can be assigned or retrieved.
Example:
class Person
{
private string _name; // Backing field
public string Name
{
get { return _name; } // Getter
set { _name = value; } // Setter
}
}
Modern C# allows auto-implemented properties:
public string Name { get; set; }
Properties improve encapsulation, maintainability, and security compared to direct field access.
23. method
A method is a function defined inside a class or struct that performs a specific action. Methods describe how an object behaves. They can calculate values, change the object’s state, or interact with other objects..
Example:
class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
}
Methods can have parameters, return values, and different access modifiers (public, private, protected). They support overloading, allowing multiple methods with the same name but different parameters.
24. constructor
A constructor is a special method. It runs automatically when an object is created. Its purpose is to initialize the object’s fields and properties.Example:
class Person
{
public string Name;
public Person(string name)
{
Name = name;
}
}
Constructors allow default values and prevent uninitialized objects. They can be overloaded to support multiple initialization scenarios.
25. destructor (finalizer)
A destructor, or finalizer, is a method. It runs automatically before an object is garbage collected. Its purpose is to clean up resources, such as file handles or database connections.
Example:
class FileHandler
{
~FileHandler()
{
Console.WriteLine(“Cleaning up resources…”);
}
}
.NET uses garbage collection to manage memory. Because of this, destructors are rarely needed. They are mainly used for handling unmanaged resources.26. static
The static keyword shows that a member, like a field, method, or property, belongs to the type itself. It does not belong to any specific object.
Example:
class MathHelper
{
public static double Pi = 3.14159; // Static field
public static int Square(int x) // Static method
{
return x * x;
}
}
Static members are shared across all instances and do not require object instantiation. A static class cannot be instantiated and can only contain static members.
27. this keyword
The this keyword refers to the current instance of a class or struct. This is used to clarify when instance members and method parameters have the same name.
Example:
class Person
{
private string name;
public void SetName(string name)
{
this.name = name; // Refers to the instance field
}
}
It is also used to chain constructors within a class.
28. base keyword
The base keyword is used in a derived class. It refers to the members of the parent class. These members include constructors, methods, and properties.
Example:
class Animal
{
public void Speak()
{
Console.WriteLine(“Animal makes a sound.”);
}
}
class Dog : Animal
{
public void Bark()
{
base.Speak(); // Calls the base class method
Console.WriteLine(“Dog barks.”);
}
}
It is also commonly used in constructor chaining:
class Mammal
{
public Mammal(string type)
{
Console.WriteLine($”Mammal: {type}”);
}
}
class Cat : Mammal
{
public Cat() : base(“Cat”) {} // Calls base constructor
}
29. inheritance
Inheritance is a principle in object-oriented programming (OOP). In this concept, a class (the child) comes from another class (the parent). This allows the child class to use and add to the parent’s features.
Example:
class Animal
{
public void Eat()
{
Console.WriteLine(“Eating…”);
}
}
class Dog : Animal // Inheriting from Animal
{
public void Bark()
{
Console.WriteLine(“Barking…”);
}
}
A child class can access public and protected members of the parent class. Inheritance promotes code reuse and hierarchical structuring of classes.
30. polymorphism
Polymorphism lets us treat objects as if they are their base class. This allows derived classes to change behavior through method overriding.Example:
class Animal
{
public virtual void Speak()
{
Console.WriteLine(“Animal makes a sound.”);
}
}
class Cat : Animal
{
public override void Speak()
{
Console.WriteLine(“Cat meaows.”);
}
}
In this case, the Speak() function is changed in the Cat class. This gives it a specific way to work while keeping the same interface.
Polymorphism allows applications to be flexible and easy to expand. It lets derived classes change how the base class behaves.
31. encapsulation
Encapsulation is a key principle of object-oriented programming (OOP). It hides data and logic from outside code. Only what is necessary is shown. This keeps the internal state of objects safe. It lets you access data through methods and properties. You cannot change data directly. Encapsulation often uses access modifiers to enforce this.
-
private (only accessible within the same class)
-
protected (accessible within the same class and derived classes)
-
public (accessible from anywhere)
-
internal (accessible within the same assembly)
Example:
class BankAccount
{
private decimal balance; // Encapsulated field
public decimal Balance
{
get { return balance; } // Controlled access
private set { balance = value; }
}
public void Deposit(decimal amount)
{
if (amount > 0)
Balance += amount;
}
}
Encapsulation improves data integrity, security, and code maintainability.
32. interface
An interface sets rules for classes. It specifies the methods, properties, and events that classes must have. However, it does not provide any implementations. Interfaces enable multiple inheritance and promote loose coupling in software design.
Example:
interface IAnimal
{
void Speak(); // Method signature without implementation
}
class Dog : IAnimal
{
public void Speak() { Console.WriteLine(“Bark!”); }
}
Interfaces let objects be used in a general way. This makes the code more flexible and easier to test.
33. struct
A struct is a lightweight value type used for small, immutable data structures. Structs are different from classes. They do not allow inheritance. Structs are stored on the stack, which makes them use memory more efficiently.
Example:
struct Point
{
public int X { get; set; }
public int Y { get; set; }
}
Structs work best for small data types. They are good for data that doesn’t change and needs high performance. Examples include coordinates and colors.