csharp

C# Interface with Programming Examples

C# Interfaces

C# Interface:- C# knows no multiple inheritances in classes. Even with abstract ones, there are no multiple inheritances for classes. C# interfaces are on the other hand C# interfaces that only contain prototypes of methods. They serve to standardize programs. Multiple inheritances are allowed.


Declaration Syntax of C# interface:

Implementing Syntax of C# interface:

C# interfaces are often an alternative to abstract classes. In the C# interface, we only find the signature of these methods; we cannot find any code (which is only possible in abstract classes). We also cannot find a qualifier like public, private or protected because it’s up to the class implementing the interface to decide. If a function of the C# interface (of which only the signature is given) accepts one or more arguments, it is necessary oddly enough to give a name to these arguments. Declare the IB interface (the custom is to start the name of an interface with I):

Classes that implement the IB interface will need to implement the functions f1 and f2. Other said, they should contain the body of these functions and it is up to you to write these instructions in classes implementing IB. A C# interface can only contain signatures, it cannot contain any function body or field (unlike abstract classes which can contain fields and function implementations). A C# interface can contain definitions of properties or indexers. For example (an indexer on an integer must be fully implemented in any class implementing the IX interface):

A C# interface can be derived from another interface. We say that a C# interface can “implement” another interface, even if it is not strictly speaking an implementation. The C# interface derivative then includes its own methods as well as those of the base interface (without forgetting that these are only signatures). Unlike classes, for which the derivation multiple is not allowed, an C# interface can derive from several interfaces: it suffices to separate by commas the names of the various basic interfaces. A class can also implement several interfaces. As a reminder, a class can only derive from a single class.



A class implementing an interface

A class indicates that it implements an interface (or several interfaces) as follows (little difference in syntax compared to class inheritance):

or, if the class implements multiple C# interfaces:

In the following program, class A implements interface IB (the order of appearance of C# interfaces, classes and Main is irrelevant):


C# interface implementation:

Output:

C# interface

Reference to an interface:

In the above example, a denotes a reference to object A. As class A implements the interface IB, a can be converted to a reference (here called ref) to the interface IB:

The IB interface methods, and only these, can now be called via ref:


A class implementing several interfaces:

If class A implements the interfaces IB and IC, it is possible that functions of IB carry the same name as functions of IC:

If class A implements the interfaces IB and IC, A must implement f of IB and f of IC. There is ambiguity since we find f in both IB and IC. Regardless of the signature (type and number of arguments) of these functions, there is ambiguity as long as two names are identical. To solve the problem, class A must specify that it implements f of A and f of B. These two functions can only be called via interface references:

To execute these functions applied to an object of class A, we must write:


How do you determine that a class implements an interface?

Consider a base class called Base (base is a reserved word, not Base), an IB interface, and a Derived class derived from Base and implementing IB interface:

An object of the Base class is created by:

 

An object of the Derived class can be created by:

but also by (since an object of a derived class is also an object of its base class):

To determine if an object of the Base class (b or d2) implements the IB interface (this is the case for d2 but not for b), we can write:

We can therefore write (the cast is necessary because d2 is officially an object of the Base class which does not implement IB):



But what if you write:

Each cell in tabBase is a Base object or an object of a class derived from Base (Derived in this case). For cells that contain a Derived object, calling f via refB is correct. On the other hand, for cells actually containing a Base object, this constitutes a serious error, with the termination of the program if the error is not caught in a try/catch. The compiler accepts the syntax because the determination of the actual content of bo can only be done at the time of running the program. A first solution is to test the contents of bo:

Another solution is to try a conversion (as operator) and check the result of the conversion (bo as IB gives null as a result when bo does not implement the IB interface):

C# Interface Programming Examples:



how to implement an interface in c#:

So in the example below the class Rectangle from the interfaces Corner and Sides be derived. Rectangle must implement all inherited methods. This ensures that the methods provided by the C# interfaces are.

 

output:

C# interface

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button