What is an interface listing for C #?

I understand how interface writing works in C #, as described, for example, here: codeguru explanation

interface IIntelligence { bool intelligent_behavior(); } class Human: IIntelligence { public Human() { //............. } /// Interface method definition in the class that implements it public bool intelligent_behavior() { Console.WriteLine("........"); return true } } 

However, I got confused in the following process of casting the interface:

 Human human = new Human(); // The human object is casted to the interface type IIntelligence humanIQ = (IIntelligence)human; humanIQ.intelligent_behavior(); 

What is the point of the class (Human in this case) implementing the interface, and then returning its human instance back to the interface? The question is not how it works, but why it is made.

+5
source share
7 answers

.net offers two types of interface implementations: implicit implementation and explicit implementation.

When you use an implicit implementation, it will become part of an interface of the type, for example, if you have an IPerson interface, for example:

 public interface IPerson { string Name{get;} } 

and you implement it as follows:

 public class Person:IPerson { public string Name{get; ....} } 

you can access it as follows (implicitly):

 aPerson.Name; 

but if you implement it like this (explicitly):

 public class Person:IPerson { string IPerson.Name{get; ....} // notice that there no need to include access modifier. } 

Then access to it is possible only through the IPerson interface:

 ((IPerson)aPerson).Name; 

UPDATE:

In fact, an explicit implementation of an interface allows us to implement different interfaces with members that have the same name. (as shown in this tutorial )

+4
source

Sometimes you may not know what an object is, but you know that it implements a certain interface.

+2
source

To access the explicit implementation of the interface.

Sometimes you want to hide the fact that a class implements an interface. This is done by implementing the interface explicitly.

 public class MyClass : IInterface { string IInterface.TheMethod(){} } 
0
source

For the same reason, you added a derived class to the base class.

0
source

A brief and popular example. We can implement this code: IIntelligence interface {string Talk (); }

  class Cat: ICreature { public string Talk() { Console.WriteLine("Meow!"); return true } } class Dog: ICreature { public string Talk() { Console.WriteLine("Arf!"); return true } } class Human: ICreature { public string Talk() { Console.WriteLine("Hello!"); return true } } 

And then we can use the following code:

 ICreature() creatures = new ICreature(){new Human(), new Dog(), new Cat()}; foreach(IIntelligence creature in creatures){ Console.WriteLine(creature.Talk()); } 

For more information, see the section "Polymorphism in object-oriented programming" on google.

0
source

Consider this situation. I have added a method to your example.

 interface IIntelligence { bool intelligent_behavior(); } class Human: IIntelligence { public Human() { } /// Interface method definition in the class that implements it public bool IIntelligence.intelligent_behavior() { Console.WriteLine("........"); return true; } //Some other method definition public bool intelligent_behaviour() { return false; } } 

You would choose IIntelligence to get the right implementation of the method.

0
source

I learned that great use of the interface is useful when developing plugins for the main application. I have created three projects. The first project, 'connectorInterface', contains only one class definition, which is inteface. Interface Code:

 public interface IConnectorDataReader { int ColumnCount { get; } bool readNextRecord(); string this[int i] { get; } void reset(); } 

The second project "dataSource1" (a plugin for the main application) implements the IConnectorDataReader interface, and the class that implements the interface has some additional private methods. The third "main application" of the project when using the plugin 'dataSource1' uses this code to read data from the plugin 'dataSource1':

  Assembly assembly = Assembly.LoadFile(path); // path to dll Type type = assembly.GetType("dataSource1.DataReader"); object myInstance = Activator.CreateInstance(type); MethodInfo method = type.GetMethod("getConnectorDataReader"); object data = method.Invoke(myInstance, null); IConnectorDataReader reader =(IConnectorDataReader)data; // method usage while (reader.readNextRecord() == true) .... 

In my case, casting is useful for reading plugin data. I don't care how the plugin is implemented as long as it implements the common interface. All that I like and use is a common method of reading data. I believe the interfaces are useful and also return to the interfaces.

0
source

Source: https://habr.com/ru/post/988768/


All Articles