0
0
C Sharp (C#)programming~10 mins

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

Choose your learning style9 modes available
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.