Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Interface declaration syntax in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Interface declaration syntax
Start
Write 'interface' keyword
Name the interface
Open curly brace '{'
Declare method/property signatures
Close curly brace '}'
Interface ready to use
This flow shows how to write an interface: start with the keyword, name it, add method signatures inside braces, then finish.
Execution Sample
C Sharp (C#)
interface IAnimal
{
    void Speak();
    int Legs { get; }
}
Declares an interface named IAnimal with a method Speak and a read-only property Legs.
Execution Table
StepCode LineActionResult
1interface IAnimalDeclare interface named IAnimalInterface IAnimal created
2{Open interface bodyReady to add members
3void Speak();Declare method signature SpeakMethod Speak added to interface
4int Legs { get; }Declare read-only property LegsProperty Legs added to interface
5}Close interface bodyInterface declaration complete
💡 Interface IAnimal declared with method Speak and property Legs signatures
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
IAnimalundefinedDeclared as interfaceHas method Speak()Has property Legs { get; }Complete interface
Key Moments - 2 Insights
Why don't we provide method bodies inside the interface?
Interfaces only declare what methods/properties must exist, not how they work. See execution_table step 3 where only the signature is declared without a body.
Can properties in interfaces have setters?
Yes, but in this example (step 4) Legs has only a getter. You can add a setter by writing { get; set; }.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is declared at step 3?
AA property Legs
BA method signature Speak()
CThe interface name
DThe interface body closes
💡 Hint
Check the 'Code Line' and 'Action' columns at step 3 in execution_table
At which step does the interface declaration finish?
AStep 2
BStep 3
CStep 5
DStep 4
💡 Hint
Look for the step where the interface body closes in execution_table
If we add a setter to Legs property, which step changes?
AStep 4
BStep 3
CStep 1
DStep 5
💡 Hint
Property declarations happen at step 4 in execution_table
Concept Snapshot
Interface declaration syntax in C#:
interface InterfaceName
{
    ReturnType MethodName();
    PropertyType PropertyName { get; set; }
}
Interfaces declare method/property signatures without bodies.
Full Transcript
This visual trace shows how to declare an interface in C#. First, write the keyword 'interface' followed by the interface name. Then open curly braces to start the body. Inside, declare method signatures like 'void Speak();' without bodies, and properties like 'int Legs { get; }' with accessors but no implementation. Finally, close the braces to complete the interface. Interfaces only specify what methods and properties a class must have, not how they work. This example declares an interface named IAnimal with a Speak method and a read-only Legs property.

Practice

(1/5)
1. What is the main purpose of an interface in C#?
easy
A. To define a contract with method and property signatures only
B. To implement all method bodies for a class
C. To store data like variables and constants
D. To create an instance of a class directly

Solution

  1. Step 1: Understand what an interface is

    An interface only declares method and property signatures without implementations.
  2. Step 2: Compare with other options

    Interfaces do not implement methods or store data; they define a contract for classes.
  3. Final Answer:

    To define a contract with method and property signatures only -> Option A
  4. Quick Check:

    Interface purpose = contract definition [OK]
Hint: Interfaces declare methods, they don't implement them [OK]
Common Mistakes:
  • Thinking interfaces contain method bodies
  • Confusing interfaces with classes
  • Believing interfaces store data
2. Which of the following is the correct syntax to declare an interface named IMyInterface in C#?
easy
A. interface IMyInterface { void MyMethod(); }
B. class IMyInterface { void MyMethod(); }
C. interface IMyInterface() { void MyMethod(); }
D. interface IMyInterface[] { void MyMethod(); }

Solution

  1. Step 1: Check the keyword and name format

    Interfaces use the keyword interface followed by the name without parentheses or brackets.
  2. Step 2: Validate method declaration inside interface

    Methods inside interfaces have only signatures ending with semicolons, no bodies.
  3. Final Answer:

    interface IMyInterface { void MyMethod(); } -> Option A
  4. Quick Check:

    Correct interface syntax = interface IMyInterface { void MyMethod(); } [OK]
Hint: Use 'interface Name { }' without parentheses or brackets [OK]
Common Mistakes:
  • Using class keyword instead of interface
  • Adding parentheses after interface name
  • Using brackets [] after interface name
3. What will be the output of the following code?
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();
  }
}
medium
A. Compilation error: Show method missing
B. Hello Interface
C. Runtime error: Cannot create interface instance
D. No output

Solution

  1. Step 1: Understand interface implementation

    The class Demo implements IExample and provides the Show method.
  2. Step 2: Analyze the Main method

    An object of Demo is created and assigned to an IExample reference, then Show() is called, printing the message.
  3. Final Answer:

    Hello Interface -> Option B
  4. Quick Check:

    Interface method call prints message [OK]
Hint: Interface methods must be implemented to avoid errors [OK]
Common Mistakes:
  • Assuming interfaces can be instantiated directly
  • Forgetting to implement interface methods
  • Expecting no output without method body
4. Identify the error in the following interface declaration:
interface ITest {
  void Run() {}
}
medium
A. Missing semicolon after method declaration
B. Method name must be lowercase
C. Interface name must start with lowercase 'i'
D. Interfaces cannot have method bodies

Solution

  1. Step 1: Check method declaration in interface

    Interfaces only declare method signatures without bodies (no curly braces).
  2. Step 2: Validate other syntax rules

    Method names can be any case; interface names usually start with uppercase 'I'. Semicolon is required after signature.
  3. Final Answer:

    Interfaces cannot have method bodies -> Option D
  4. Quick Check:

    Interface methods = signatures only [OK]
Hint: No method bodies allowed inside interfaces [OK]
Common Mistakes:
  • Adding method bodies inside interfaces
  • Confusing naming conventions with syntax errors
  • Omitting semicolon after method signature
5. You want to declare an interface 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?
hard
A. interface IVehicle { void Start(); void Stop(); } class Car { public void Start() { } public void Stop() { } }
B. interface IVehicle { void Start() {} void Stop() {} } class Car : IVehicle { }
C. interface IVehicle { void Start(); void Stop(); } class Car : IVehicle { public void Start() { Console.WriteLine("Car started"); } public void Stop() { Console.WriteLine("Car stopped"); } }
D. interface IVehicle { void Start(); void Stop(); } class Car : IVehicle { void Start() { } void Stop() { } }

Solution

  1. Step 1: Declare interface with method signatures only

    IVehicle must declare Start() and Stop() without bodies.
  2. Step 2: Implement interface methods publicly in class

    Car must implement both methods with public access and provide method bodies.
  3. 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 lacks public modifier, causing error.
  4. Final Answer:

    Correct interface and class implementation with public methods -> Option C
  5. Quick Check:

    Interface methods declared; class implements publicly [OK]
Hint: Interface methods need public implementation in classes [OK]
Common Mistakes:
  • Adding method bodies inside interface
  • Not implementing interface in class
  • Omitting public modifier in class methods