
C# Interview Questions on Interface
In this article, you can find the most important C# Interview Questions on Interface.
What is an interface in C#?
An interface is a programming structure that defines a set of methods, properties, and events that a class or struct can implement.
How is an interface different from a class?
An interface contains only method, property, and event declarations, whereas a class can contain both declarations and implementations. A class can implement multiple interfaces, but it can inherit only one class.
Write a program to demostrate interface in C#
public interface IShape { void Draw(); int GetArea(); }
In this example, we define an interface named IShape that declares two methods: Draw() and GetArea(). Any class or struct that implements this interface must provide an implementation for these two methods.
For example, here is a class that implements the IShape interface:
public class Rectangle : IShape { private int _length; private int _width; public Rectangle(int length, int width) { _length = length; _width = width; } public void Draw() { Console.WriteLine("Drawing a rectangle..."); } public int GetArea() { return _length * _width; } }
In this example, the Rectangle class implements the IShape interface, providing an implementation for the Draw() and GetArea() methods. Any code that expects an IShape object can use a Rectangle object, since it implements the interface.
Can an interface contain fields?
No, an interface cannot contain fields, only method, property, and event declarations.
Can you provide an example of how to define an interface in C#?
public interface IShape { void Draw(); int GetArea(); }
Can an interface inherit from another interface?
Yes, an interface can inherit from one or more other interfaces using the : operator.
Can an interface implement another interface?
No, an interface cannot implement another interface, only a class can implement an interface.
Can an interface inherit another interface?
Yes, an interface can inherit from one or more other interfaces using the : operator. This allows the derived interface to include all the members of the base interface(s), in addition to any new members that it defines.
Here is an example of an interface that inherits from another interface:
public interface IShape { void Draw(); } public interface IShapeWithArea : IShape { int GetArea(); }
In this example, the IShapeWithArea interface inherits from the IShape interface using the : operator. This means that any class or struct that implements the IShapeWithArea interface must also implement the Draw() method from the IShape interface, in addition to the GetArea() method that is defined in the IShapeWithArea interface.
How do you implement an interface in a class in C#?
To implement an interface in a class, you use the : operator followed by the name of the interface. Then you provide the implementation for each method, property, and event declared in the interface.
Here is an example:
public class Circle : IShape { public void Draw() { // implementation code here } public int GetArea() { // implementation code here } }
Can a struct implement an interface in C#?
Yes, a struct can implement an interface in C#. In fact, a struct can implement any interface that a class can implement. To implement an interface in a struct, you follow the same syntax as you would for a class. You specify the interface name after the : character and then provide the implementation for each member of the interface.
Here is an example of a struct that implements the IShape interface:
public struct Rectangle : IShape { private int _length; private int _width; public Rectangle(int length, int width) { _length = length; _width = width; } public void Draw() { Console.WriteLine("Drawing a rectangle..."); } public int GetArea() { return _length * _width; } }
In this example, the Rectangle struct implements the IShape interface, which requires the implementation of the Draw() and GetArea() methods. The implementation of these methods is provided in the struct.
Can an interface have access modifiers?
No, an interface cannot have access modifiers on its members. All members of an interface are implicitly public and abstract, meaning they are accessible to any class or struct that implements the interface, and they do not have an implementation. The purpose of an interface is to define a contract or a set of requirements that must be implemented by any class or struct that wants to use it. Therefore, access modifiers are not necessary on the members of an interface.
Can an interface contain implementation code?
No, an interface cannot contain implementation code. It can only contain method, property, and event declarations. The implementation of these members must be provided by the class or struct that implements the interface. The purpose of an interface is to define a contract for a class or struct, and the implementation details are left up to the implementing class or struct.
– Article ends here –