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

Implementing interfaces in C Sharp (C#) - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare that the class implements the interface.

C Sharp (C#)
public interface IAnimal {
    void Speak();
}

public class Dog : [1] {
    public void Speak() {
        Console.WriteLine("Woof!");
    }
}
Drag options to blanks, or click blank then click option'
AIAnimal
Bclass
Cinterface
DAnimal
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of the interface name after the colon.
Trying to use 'interface' keyword after the class name.
2fill in blank
medium

Complete the code to implement the interface method correctly.

C Sharp (C#)
public interface IShape {
    double GetArea();
}

public class Square : IShape {
    private double side;
    public Square(double side) {
        this.side = side;
    }
    public double [1]() {
        return side * side;
    }
}
Drag options to blanks, or click blank then click option'
AArea
BGetArea
CCalculateArea
DArea()
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than the interface.
Adding parentheses in the method name in the declaration.
3fill in blank
hard

Fix the error by completing the code to explicitly implement the interface method.

C Sharp (C#)
public interface ILogger {
    void Log(string message);
}

public class ConsoleLogger : ILogger {
    void [1].Log(string message) {
        Console.WriteLine(message);
    }
}
Drag options to blanks, or click blank then click option'
AILogger
BLog
CConsoleLogger
DLogger
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name instead of the interface name.
Omitting the interface name prefix.
4fill in blank
hard

Fill both blanks to complete the interface and class implementation with a property.

C Sharp (C#)
public interface ICar {
    int [1] { get; set; }
}

public class Sedan : ICar {
    private int speed;
    public int [2] {
        get { return speed; }
        set { speed = value; }
    }
}
Drag options to blanks, or click blank then click option'
ASpeed
Bspeed
DVelocity
Attempts:
3 left
💡 Hint
Common Mistakes
Using different property names in interface and class.
Using lowercase property name in interface but uppercase in class.
5fill in blank
hard

Fill all three blanks to implement multiple interfaces with methods.

C Sharp (C#)
public interface IFlyable {
    void [1]();
}

public interface ISwimmable {
    void [2]();
}

public class Duck : [3] {
    public void Fly() {
        Console.WriteLine("Flying");
    }
    public void Swim() {
        Console.WriteLine("Swimming");
    }
}
Drag options to blanks, or click blank then click option'
AFly
BSwim
CIFlyable, ISwimmable
DDuck
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name instead of interface names after the colon.
Mismatching method names with interface declarations.

Practice

(1/5)
1. What does it mean to implement an interface in C#?
easy
A. A class provides code for all methods declared in the interface.
B. An interface inherits from a class.
C. A class hides all methods of the interface.
D. An interface creates objects directly.

Solution

  1. Step 1: Understand interface purpose

    An interface declares methods without code, setting a contract.
  2. Step 2: Implementing means coding methods

    A class that implements the interface must write the code for all those methods.
  3. Final Answer:

    A class provides code for all methods declared in the interface. -> Option A
  4. Quick Check:

    Implementing interface = writing required methods [OK]
Hint: Implementing means writing all interface methods in the class [OK]
Common Mistakes:
  • Thinking interfaces can create objects
  • Believing interfaces inherit from classes
  • Assuming methods are hidden, not implemented
2. Which of the following is the correct syntax to implement an interface IMyInterface in a class MyClass?
easy
A. interface MyClass : IMyInterface { }
B. class MyClass : IMyInterface { }
C. class MyClass implements IMyInterface { }
D. class MyClass inherits IMyInterface { }

Solution

  1. Step 1: Recall C# interface syntax

    In C#, a class implements an interface using a colon (:), not 'implements' or 'inherits'.
  2. Step 2: Check each option

    class MyClass : IMyInterface { } uses correct syntax: class MyClass : IMyInterface { }. Others use wrong keywords or declare interface as class.
  3. Final Answer:

    class MyClass : IMyInterface { } -> Option B
  4. Quick Check:

    Use colon (:) to implement interface [OK]
Hint: Use ':' to implement interface, not 'implements' or 'inherits' [OK]
Common Mistakes:
  • Using 'implements' keyword like Java
  • Trying to inherit interface with 'inherits'
  • Declaring interface as a class
3. What will be the output of this code?
interface IGreet { void SayHello(); }
class Person : IGreet {
public void SayHello() { Console.WriteLine("Hi!"); }
}
var p = new Person();
p.SayHello();
medium
A. No output
B. SayHello
C. Hi!
D. Compile error

Solution

  1. Step 1: Understand interface and class

    The interface IGreet requires SayHello method. Person implements it by printing "Hi!".
  2. Step 2: Trace code execution

    Creating Person object and calling SayHello prints "Hi!" to console.
  3. Final Answer:

    Hi! -> Option C
  4. Quick Check:

    Implemented method runs and prints output [OK]
Hint: Implemented method runs exactly as coded in class [OK]
Common Mistakes:
  • Expecting method name printed instead of output
  • Thinking interface prints output
  • Assuming compile error without method body
4. Identify the error in this code:
interface IRun { void Run(); }
class Animal : IRun { }
medium
A. Class Animal must implement Run method.
B. Interface IRun cannot be empty.
C. Class Animal cannot implement interface.
D. No error, code is correct.

Solution

  1. Step 1: Check interface requirements

    IRun interface requires a method Run() to be implemented.
  2. Step 2: Verify class implementation

    Animal class implements IRun but does not provide Run() method, causing error.
  3. Final Answer:

    Class Animal must implement Run method. -> Option A
  4. Quick Check:

    Implement all interface methods [OK]
Hint: All interface methods must be in the class [OK]
Common Mistakes:
  • Forgetting to implement interface methods
  • Thinking interfaces must have code
  • Assuming empty class is allowed
5. You have two interfaces:
interface IWalk { void Walk(); }
interface ITalk { void Talk(); }

How can a class Human implement both interfaces correctly?
hard
A. interface Human : IWalk, ITalk { }
B. class Human : IWalk { public void Walk() { } } class Human : ITalk { public void Talk() { } }
C. class Human implements IWalk, ITalk { }
D. class Human : IWalk, ITalk { public void Walk() { Console.WriteLine("Walking"); } public void Talk() { Console.WriteLine("Talking"); } }

Solution

  1. Step 1: Understand multiple interface implementation

    A class can implement multiple interfaces by listing them separated by commas.
  2. Step 2: Provide all required methods

    Human class must provide Walk() and Talk() methods with code.
  3. Final Answer:

    class Human : IWalk, ITalk { public void Walk() { Console.WriteLine("Walking"); } public void Talk() { Console.WriteLine("Talking"); } } -> Option D
  4. Quick Check:

    Multiple interfaces implemented with all methods [OK]
Hint: Separate interfaces with commas and implement all methods [OK]
Common Mistakes:
  • Trying to declare multiple classes with same name
  • Using 'implements' keyword (Java style)
  • Declaring interface instead of class