C# Class

C# Interface Example with Properties

An interface is a class model. It contains methods and properties, with the particularity that it does not provide the implementation of methods. This means that we just describe the methods without describing what they do.

An interface is useless on its own: each interface is doomed to be implemented by a class (or a structure) which will have to implement its methods and have its properties. The purpose of an interface is to define the methods and properties offered by any class (or structure) that implements it, without the developer needing to know how they are coded. To put it simply, an interface is like a contract: it defines expectations without planning the technical solutions that will be used.

Example 1:

Example 2:


Create an interface

The declaration of an interface is similar to that of a class, with a few differences. In particular, we use the interface keyword instead of the class keyword.

In terms of accessibility, an interface is always public, so you cannot use an access restrictor (public, private, protected or internal)

Here is an example interface:

Thanks to this, we know that an object stored in the form of IAnimal will always have the Name property accessible for reading and the Move method.

You just have to put get; and not get {return m_name; } because the interface does not know m_name and does not care. Again, what matters are the features available, not how they are coded. Besides, who told you that the developer would name a field with m_ in front of it and not just _ as some do?

Use an existing interface

Any class implementing an interface must implement its methods (with the same signatures) and its properties. In case you forgot, signing a method identifies it; it depends on three things: its name, its return type and its arguments.

Let’s go back to the previous example. The implementation of Move must therefore also be named Move, return void, and take no parameter as input.

Here is an example :

You can manipulate objects with the type of the interface: you don’t have to use the type of a class that implements it; you can therefore put in the Main:

And it will display Snowy moves.in console.
As you can see, it is possible to store an object of type Dogin another of type IAnimal, since it Dogimplements the interface IAnimal.

Use multiple interfaces

Depending on your needs, you can even use multiple interfaces. To do this, you have to separate the interfaces with commas, as in the following example (where SampleClassimplements both IControland ISurface):

In this case, if both interfaces describe a method with the same signature, they will both call the same method. See instead:

In both cases, the same method Paintis called. To do two different methods, replace the code SampleClass with:

You notice in passing that I did not put an accessor in front of the two methods although I had put one publicearlier. Since this time these are methods that each implement a specific interface, they are necessarily public because they can only be used from interfaces.
You can then do:

would produce a compilation error because we did not create a “general” method Paint. It is however doable if we implement a Paint“general”:

In this case, c.Paint();call again IControl.Paintand do not call the “general” method precisely because we have made a method specially for the interface.

At the moment you probably do not see all the usefulness of interfaces; but by working on real projects you may have the opportunity to use them. Anyway, in this tutorial we will need it, although you won’t necessarily see a lot of it right away.

Source:

https://openclassrooms.com/fr/courses/218202-apprenez-a-programmer-en-c-sur-net/217900-les-interfaces

https://www.geeksforgeeks.org/c-sharp-interface/

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.