C# 0 The Complete Reference


new . Explicit Implementations



Download 4,07 Mb.
Pdf ko'rish
bet418/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   414   415   416   417   418   419   420   421   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

new

.

Explicit Implementations

When implementing a member of an interface, it is possible to 

fully qualify

 its name with 

its interface name. Doing this creates an 

explicit interface member implementation,

 or 


explicit

implementation,

 for short. For example, given

interface IMyIF {

  int MyMeth(int x);

}

then it is legal to implement 



IMyIF

 as shown here:

class MyClass : IMyIF {

  int IMyIF.MyMeth(int x) {

    return x / 3;

  }


}

As you can see, when the 



MyMeth( )

 member of 



IMyIF

 is implemented, its complete name, 

including its interface name, is specified.

There are two reasons that you might need to create an explicit implementation of an 

interface method. First, when you implement an interface method using its fully qualified 

name, you are providing an implementation that 



cannot

 be accessed through an object of the 

class. Instead, it must be accessed via an interface reference. Thus, an explicit implementation 

gives you a way to implement an interface method so that it is not a public member of the 

www.freepdf-books.com



324

 

P a r t   I :  



T h e   C #   L a n g u a g e

class that provides the implementation. Second, it is possible for a class to implement two 

interfaces, both of which declare methods by the same name and type signature. Qualifying 

the names with their interfaces removes the ambiguity from this situation. Let’s look at an 

example of each.

The following program contains an interface called 



IEven

, which defines two methods, 



IsEven( )

 and 


IsOdd( )

, which determine if a number is even or odd. 



MyClass

 then implements 



IEven

. When it does so, it implements 



IsOdd( )

 explicitly.

// Explicitly implement an interface member.

using System;

interface IEven {

  bool IsOdd(int x);

  bool IsEven(int x);

}

class MyClass : IEven {



  // Explicit implementation. Notice that this member is private

  // by default.

  bool IEven.IsOdd(int x) {

    if((x%2) != 0) return true;

    else return false;

  }


  // Normal implementation.

  public bool IsEven(int x) {

    IEven o = this; // Interface reference to the invoking object.

    return !o.IsOdd(x);

  }

}

class Demo {



  static void Main() {

    MyClass ob = new MyClass();

    bool result;

    result = ob.IsEven(4);

    if(result) Console.WriteLine("4 is even.");

    // result = ob.IsOdd(4); // Error, IsOdd not exposed.

    // But, this is OK. It creates an IEven reference to a MyClass object

    // and then calls IsOdd() through that reference.

    IEven iRef = (IEven) ob;

    result = iRef.IsOdd(3);

    if(result) Console.WriteLine("3 is odd.");

  }


}

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   414   415   416   417   418   419   420   421   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish