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

Interface declaration syntax in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an interface in C#?
An interface in C# is a contract that defines a set of methods and properties without implementing them. Classes or structs that implement the interface must provide the implementation.
Click to reveal answer
beginner
How do you declare an interface in C#?
Use the interface keyword followed by the interface name and a block containing method/property signatures. For example:<br>
public interface IExample { void DoWork(); }
Click to reveal answer
intermediate
Can interfaces contain fields or constructors in C#?
No, interfaces cannot contain fields or constructors. They only declare method, property, event, or indexer signatures without implementation.
Click to reveal answer
intermediate
What access modifier is used for interface members in C#?
Interface members are implicitly public and cannot have access modifiers. All members declared in an interface are public by default.
Click to reveal answer
beginner
How does a class implement an interface in C#?
A class implements an interface by using a colon <code>:</code> followed by the interface name, then providing implementations for all interface members. Example:<br><pre>public class Worker : IExample { public void DoWork() { /* code */ } }</pre>
Click to reveal answer
Which keyword is used to declare an interface in C#?
Ainterface
Bclass
Cstruct
Dimplements
Can an interface in C# contain method implementations?
AYes, always
BOnly default interface methods (C# 8.0+)
COnly private methods
DNo, never
What access modifier do interface members have by default?
Apublic
Bprotected
Cprivate
Dinternal
How does a class indicate it implements an interface?
AUsing the keyword 'inherits'
BUsing the keyword 'implements'
CUsing a colon ':' followed by the interface name
DUsing angle brackets '<>'
Which of these can NOT be declared inside an interface?
AEvents
BProperties
CMethod signatures
DFields
Explain how to declare an interface in C# and how a class implements it.
Think about the contract nature of interfaces and how classes fulfill that contract.
You got /4 concepts.
    What are the limitations of interfaces in C# regarding members they can contain?
    Consider what an interface represents and what it cannot do.
    You got /4 concepts.