An interface in C# is like a promise that a class will have certain methods or properties. It helps organize code and makes sure different parts work well together.
Interface declaration syntax in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
interface InterfaceName { // Method signatures ReturnType MethodName(ParameterList); // Property signatures PropertyType PropertyName { get; set; } }
The keyword interface starts the declaration.
Interfaces only have method and property signatures, no actual code inside methods.
Speak method.interface IAnimal { void Speak(); }
Wheels property and a Drive method.interface IVehicle { int Wheels { get; set; } void Drive(); }
interface IShape { double Area(); double Perimeter(); }
This program shows an interface IGreet with a method SayHello. Two classes, Person and Robot, implement this interface with their own versions of SayHello. The Main method calls these methods through the interface.
using System; interface IGreet { void SayHello(); } class Person : IGreet { public void SayHello() { Console.WriteLine("Hello from Person!"); } } class Robot : IGreet { public void SayHello() { Console.WriteLine("Beep boop, hello from Robot!"); } } class Program { static void Main() { IGreet person = new Person(); IGreet robot = new Robot(); person.SayHello(); robot.SayHello(); } }
Interfaces cannot contain fields or constructors.
A class can implement multiple interfaces, helping organize different behaviors.
Interfaces help with writing flexible and testable code.
Interfaces define a contract with method and property signatures only.
Classes use interfaces to promise they have certain methods or properties.
This helps write organized, flexible, and reusable code.
Practice
interface in C#?Solution
Step 1: Understand what an interface is
An interface only declares method and property signatures without implementations.Step 2: Compare with other options
Interfaces do not implement methods or store data; they define a contract for classes.Final Answer:
To define a contract with method and property signatures only -> Option AQuick Check:
Interface purpose = contract definition [OK]
- Thinking interfaces contain method bodies
- Confusing interfaces with classes
- Believing interfaces store data
IMyInterface in C#?Solution
Step 1: Check the keyword and name format
Interfaces use the keywordinterfacefollowed by the name without parentheses or brackets.Step 2: Validate method declaration inside interface
Methods inside interfaces have only signatures ending with semicolons, no bodies.Final Answer:
interface IMyInterface { void MyMethod(); } -> Option AQuick Check:
Correct interface syntax = interface IMyInterface { void MyMethod(); } [OK]
- Using class keyword instead of interface
- Adding parentheses after interface name
- Using brackets [] after interface name
interface IExample { void Show(); }
class Demo : IExample {
public void Show() { Console.WriteLine("Hello Interface"); }
}
class Program {
static void Main() {
IExample obj = new Demo();
obj.Show();
}
}Solution
Step 1: Understand interface implementation
The classDemoimplementsIExampleand provides theShowmethod.Step 2: Analyze the Main method
An object ofDemois created and assigned to anIExamplereference, thenShow()is called, printing the message.Final Answer:
Hello Interface -> Option BQuick Check:
Interface method call prints message [OK]
- Assuming interfaces can be instantiated directly
- Forgetting to implement interface methods
- Expecting no output without method body
interface ITest {
void Run() {}
}Solution
Step 1: Check method declaration in interface
Interfaces only declare method signatures without bodies (no curly braces).Step 2: Validate other syntax rules
Method names can be any case; interface names usually start with uppercase 'I'. Semicolon is required after signature.Final Answer:
Interfaces cannot have method bodies -> Option DQuick Check:
Interface methods = signatures only [OK]
- Adding method bodies inside interfaces
- Confusing naming conventions with syntax errors
- Omitting semicolon after method signature
IVehicle with two methods: Start() and Stop(). Which of the following is the correct way to declare it and implement it in a class Car?Solution
Step 1: Declare interface with method signatures only
IVehiclemust declareStart()andStop()without bodies.Step 2: Implement interface methods publicly in class
Carmust implement both methods withpublicaccess and provide method bodies.Step 3: Check other options for errors
interface IVehicle { void Start() {} void Stop() {} } class Car : IVehicle { } has method bodies in interface (invalid). interface IVehicle { void Start(); void Stop(); } class Car { public void Start() { } public void Stop() { } } does not implement interface. interface IVehicle { void Start(); void Stop(); } class Car : IVehicle { void Start() { } void Stop() { } } implements methods but lackspublicmodifier, causing error.Final Answer:
Correct interface and class implementation with public methods -> Option CQuick Check:
Interface methods declared; class implements publicly [OK]
- Adding method bodies inside interface
- Not implementing interface in class
- Omitting public modifier in class methods
