Interface in C#:-
It is a special component of c# which will contain a set of abstract methods to writing program code which will be implemented into class. The interface will be declared by the interface keyword.
The interface is mostly used to declare a set of rules, multiple inheritances, and data abstraction.
Interface decides how many features will be implemented into class for example in Car how many features should be defined that will be managed by Interface.
We can not create an object of interface hence interface will provide data hiding that is called data abstraction.
We can implement two different interfaces in a single class that is called Multiple Inheritance
note:- all method of interface is public and abstract by default in c#
Syntax of Interface:-
interface Intefacename
{
void methodname1();
void methodname2();
void methodname3();
}
class Classname:Interfacename
{
public void methodname1()
{
}
public void methodname2()
{
}
public void methodname3()
{
}
}
Example of Interface:-
interface Area
{
void rect();
void triangle();
}
interface MathMatical
{
void addition();
void substraction();
}
class AreaLogic : Area, MathMatical
{
public void rect()
{
Console.WriteLine("Rect");
}
public void triangle()
{
Console.WriteLine("Triangle");
}
public void addition()
{
Console.WriteLine("Addition");
}
public void substraction()
{
Console.WriteLine("Sub");
}
static void Main()
{
Area obj = new AreaLogic();
obj.rect();
obj.triangle();
MathMatical obj1 = new AreaLogic();
obj1.addition();
obj1.substraction();
Console.ReadKey();
}
}
}
It is a special component of c# which will contain a set of abstract methods to writing program code which will be implemented into class. The interface will be declared by the interface keyword.
The interface is mostly used to declare a set of rules, multiple inheritances, and data abstraction.
Interface decides how many features will be implemented into class for example in Car how many features should be defined that will be managed by Interface.
We can not create an object of interface hence interface will provide data hiding that is called data abstraction.
We can implement two different interfaces in a single class that is called Multiple Inheritance
note:- all method of interface is public and abstract by default in c#
Syntax of Interface:-
interface Intefacename
{
void methodname1();
void methodname2();
void methodname3();
}
class Classname:Interfacename
{
public void methodname1()
{
}
public void methodname2()
{
}
public void methodname3()
{
}
}
Example of Interface:-
interface Area
{
void rect();
void triangle();
}
interface MathMatical
{
void addition();
void substraction();
}
class AreaLogic : Area, MathMatical
{
public void rect()
{
Console.WriteLine("Rect");
}
public void triangle()
{
Console.WriteLine("Triangle");
}
public void addition()
{
Console.WriteLine("Addition");
}
public void substraction()
{
Console.WriteLine("Sub");
}
static void Main()
{
Area obj = new AreaLogic();
obj.rect();
obj.triangle();
MathMatical obj1 = new AreaLogic();
obj1.addition();
obj1.substraction();
Console.ReadKey();
}
}
}
Example of Interface using Car and Sales?
interface CarDesign
{
void CarExteriorDesign();
void CarInteriorDesign();
void CarSecurity();
}
interface SalesRules
{
void SalesPolicy();
}
class Car : CarDesign,SalesRules
{
public void CarExteriorDesign()
{
Console.WriteLine("Exterior Design");
}
public void CarInteriorDesign()
{
Console.WriteLine("Interior Design");
}
public void CarSecurity()
{
Console.WriteLine("Security");
}
public void SalesPolicy()
{
Console.WriteLine("Sales Policy");
}
static void Main()
{
Car obj = new Car();
obj.CarInteriorDesign();
obj.CarExteriorDesign();
obj.CarSecurity();
Console.ReadKey();
}
}
interface I
{
void fun(); // body null
}
interface A
{
void fun(); //body null
}
class D
{
}
class C : D,I,A
{
void I.fun()
{
Console.WriteLine("Fun Example I");
}
void A.fun()
{
Console.WriteLine("Fun Example A");
}
static void Main()
{
A obj = new C();
obj.fun();
I obj1 = new C();
obj1.fun();
Console.ReadKey();
}
}
Indexers in C# allow an object to be accessed like an array using index notation ([]), even though it’s a normal class.
🔍 Simple Definition
An indexer lets you access class data using an index just like arrays.
💡 Why Indexers Are Used
Normally, you access class data using methods:
obj.GetValue(0);
With indexers, you can do:
obj[0]; // cleaner and easier
🧠 Syntax of Indexer
class Sample
{
private int[] arr = new int[5];
public int this[int index]
{
get { return arr[index]; }
set { arr[index] = value; }
}
}
Example
class Student
{
private string[] names = new string[3];
public string this[int index]
{
get { return names[index]; }
set { names[index] = value; }
}
}
class Program
{
static void Main()
{
Student s = new Student();
s[0] = "Shiva";
s[1] = "Rahul";
Console.WriteLine(s[0]); // Shiva
}
}
🎯 Key Points
Uses keyword
thisCan have get and set
Can be overloaded (multiple indexers)
Makes code clean and readable
Works like arrays but with custom logic
🔥 Real-Life Example
Think of a Library system:
library[1] // gives book at index 1
Instead of:
library.GetBook(1);
⚡ Interview Answer
“An indexer in C# allows objects to be indexed like arrays using the
thiskeyword, providing a simpler way to access internal data.”

0 Comments
POST Answer of Questions and ASK to Doubt