The basic course of interface based on C # four

Source: Internet
Author: User
Tags count ibase inheritance interface access

Section fourth, Access interface

access to interface members

The same is true of the invocation of an interface method and the rules that take access to an index indicator as in a class. If the name of the underlying member is the same as the high-level member inherited, the underlying member overwrites the top-level member with the same name. However, because the interface supports multiple inheritance, in multiple inheritance, if two parent interfaces contain members of the same name, this produces two of semantics (which is one of the reasons why C # removes multiple inheritance mechanisms for classes), which requires explicit definition:


Using System;
Interface Isequence {
int Count {get; set;}
Interface Iring {
void Count (int i);
Interface Iringsequence:isequence, iring {}
Class CTest {
void Test (Iringsequence rs) {
Rs. Count (1); Error, Count has two meanings
Rs. Count = 1; Error, Count has two meanings
((isequence) RS). Count = 1; That's right
((iring) RS). Count (1); Call Iring.count correctly

in the example above, the first two statements are Rs. Count (1) and Rs. Count = 1 causes a compile-time error because of two semantics, so you must explicitly assign the parent interface type to RS, which does not incur additional overhead at run time.

look at the following example:

Using System;
Interface Iinteger {
void Add (int i);
Interface Idouble {
void Add (double D);
Interface Inumber:iinteger, idouble {}
Class Cmytest {
void Test (Inumber Num) {
Num.add (1); Error
Num.add (1.0); That's right
((Iinteger) n). ADD (1); That's right
((idouble) n). ADD (1); That's right

calling Num.add (1) results in two semantics because the parameter types of the candidate overloaded methods apply. However, calling Num.add (1.0) is allowed because 1.0 is inconsistent with the parameter type of the floating-point number parameter type and method Iinteger.add (), at which point only idouble.add is applicable. However, as long as the explicit assignment is added, no two semantics will be generated.

problems with multiple inheritance of interfaces can also result in member access issues. For example:

Interface IBase {
void Fway (int i);
Interface Ileft:ibase {
new void Fway (int i);
Interface Iright:ibase
{void G ();}
Interface Iderived:ileft, IRight {}
Class CTest {
void Test (iderived d) {
D. Fway (1); Call ILeft. Fway
((IBase) d). Fway (1); Call IBase. Fway
((ILeft) d). Fway (1); Call ILeft. Fway
((IRight) d). Fway (1); Call IBase. Fway

Total 3 page: previous 1 [2] [3] Next page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.