Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the implementation of interface in C #

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to understand the implementation of the interface in C#, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

There are two ways to implement interfaces in C#: implicit interfaces and explicit interfaces:

Implicitly implement interface members

Create an interface, IChinese, containing a member Speak;, and we create a class Speaker that implements the interface Chinese

/ / implicit implementation example public interface IChinese {string Speak ();} public class Speaker: IChinese {public string Speak () {return "Chinese";}}

This is the implicit implementation interface.

The implicit implementation invokes as follows:

IChinese s = new Speaker (); s.Speak (); Speaker s = new Speaker (); s.Speak ()

You can call the Speak method.

Create an interface, IEnglish, containing a member Speak; to let our class Speaker implement interface IEnglish

/ / explicit implementation example public interface IEnglish {string Speak ();} public class Speaker: IEnglish {string English.Speak () {return "English";}}

It is realized through this display interface. The Speak method can only be called through the interface:

IEnglish s = new Speaker (); s.Speak ()

The following approach will result in a compilation error:

Speaker s = new Speaker (); s.Speak ()

The difference between implicit implementation and display implementation:

I. the difference between grammar

1. The Speak implementation of the implicit Speaker has and must have its own access modifier (public), while the member of the display implementation Speaker (Speak) cannot have any access modifiers. 2. Display implementation Speaker uses the interface name and a period to name the class member (Speak): English.Speak (); that is

Second, the difference between calls

For calls implemented by implicit interfaces, note the declaration of the class, which can be declared either by the interface or by the implementation class Speaker. All callers can get the behavior Speak that invokes the instantiated object

Class Program {static void Main (string [] args) {IChinese s = new Speaker (); s.Speak (); Speaker s = new Speaker (); s.Speak ();}}

The explicit interface implements the call. Note the class declaration. Only by using the interface declaration can the caller get the behavior Speak that invokes the instantiated object.

Class Program {static void Main (string [] args) {IEnglish s = new Speaker (); s.Speak (); / / the following writing will cause a compilation error "PetShop.Speaker" does not contain the definition of "Speak"; / / Speaker s = new Speaker (); / / s.Speak ();}}

Conclusion:

The implied implementation object declares that both the interface and the class can access its behavior, showing that the implementation can only be accessed by the declared interface.

If you have the same method name in two interfaces, the class that implements both interfaces will be uncertain, and when you write the method, you don't know which interface's method to implement. In order to solve this problem, C# provides the implementation technology of display interface, that is, the interface name is added before the method name, and the member is qualified by the interface name. Use "interface name. method name ()" to distinguish which interface is implemented.

Note: when displaying the interface implementation, you cannot add any access modifiers before the method name. This method is different from the ordinary method, which is private by default without an access modifier, while the explicit interface is implemented without any modifier before the method, which is public by default. If you add a modifier before it, a compilation error will occur.

The correct way to call the display interface implementation is through the interface reference, through the interface reference to determine the version to be called.

Let's take a look at the complete example:

Using System;public interface IPerson {string Name {get; set;} void Show (string name);} public interface IStudent {string StudentId {get; set;} void Show (string studentid);} public class Student: IPerson, IStudent {private string _ name; public string Name {get {return _ name;} set {_ name = value }} private string _ studentid; public string StudentId {get {return _ studentid;} set {_ studentid = value;}} void IPerson.Show (string name) {Console.WriteLine ("name is {0}", name);} void IStudent.Show (string studentid) {Console.WriteLine ("student ID is {0}", studentid) }} class Program {static void Main () {Student s = new Student (); Console.WriteLine ("enter name"); s.Name = Console.ReadLine (); Console.WriteLine ("enter student ID"); s.StudentId = Console.ReadLine (); IPerson per = s; per.Show (s.Name); IStudent stu = s; stu.Show (s.StudentId);}}

After reading the above, have you mastered how to understand the implementation of the interface in C #? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report