Bird
0
0

Which of the following correctly defines a default method inside an interface in C#?

easy📝 Syntax Q3 of 15
C Sharp (C#) - Interfaces
Which of the following correctly defines a default method inside an interface in C#?
Ainterface IExample { void Show() => Console.WriteLine("Hello"); }
Binterface IExample { default void Show() { Console.WriteLine("Hello"); } }
Cinterface IExample { void Show(); default { Console.WriteLine("Hello"); } }
Dinterface IExample { void Show() { Console.WriteLine("Hello"); } }
Step-by-Step Solution
Solution:
  1. Step 1: Understand default interface method syntax

    In C#, default interface methods are declared by providing a method body directly inside the interface without the 'default' keyword.
  2. Step 2: Analyze each option

    interface IExample { void Show() => Console.WriteLine("Hello"); } correctly uses the expression-bodied method syntax inside the interface. interface IExample { default void Show() { Console.WriteLine("Hello"); } } incorrectly uses 'default' keyword which is not valid. interface IExample { void Show(); default { Console.WriteLine("Hello"); } } tries to separate declaration and body incorrectly. interface IExample { void Show() { Console.WriteLine("Hello"); } } attempts to define a method with a block body but without a semicolon, which is invalid in interfaces.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Default interface methods require method body without 'default' keyword [OK]
Quick Trick: Default methods have bodies directly in interface [OK]
Common Mistakes:
MISTAKES
  • Using 'default' keyword before method
  • Trying to define method body without expression syntax
  • Omitting method body in default method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes