C# 0 The Complete Reference


PART I C h a p t e r   1 9



Download 4,07 Mb.
Pdf ko'rish
bet736/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   732   733   734   735   736   737   738   739   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   1 9 :  

L I N Q  

583


PART IPART I

better solution in many cases), it is still important that you understand how they work 

because of their integral importance to LINQ.

An extension method is a static method that must be contained within a static, non-generic 

class. The type of its first parameter determines the type of objects on which the extension 

method can be called. Furthermore, the first parameter must be modified by 



this

. The object on 

which the method is invoked is passed automatically to the first parameter. It is not explicitly 

passed in the argument list. A key point is that even though an extension method is declared 



static

, it can still be called on an object, just as if it were an instance method.

Here is the general form of an extension method:

static


ret-type name

(this


invoked-on-type ob

,

param-list

)

Of course, if there are no arguments other than the one passed implicitly to 



ob,

 then 


param-list

will be empty. Remember, the first parameter is automatically passed the object on which 

the method is invoked. In general, an extension method will be a public member of its class.

Here is an example that creates three simple extension methods:

// Create and use some extension methods.

using System;

static class MyExtMeths {

  // Return the reciprocal of a double.

  public static double Reciprocal(this double v) {

    return 1.0 / v;

  }

  // Reverse the case of letters within a string and



  // return the result.

  public static string RevCase(this string str) {

    string temp = "";

    foreach(char ch in str) {

      if(Char.IsLower(ch)) temp += Char.ToUpper(ch);

      else temp += Char.ToLower(ch);

    }

    return temp;



  }

  // Return the absolute value of n / d.

  public static double AbsDivideBy(this double n, double d) {

    return Math.Abs(n / d);

  }

}

class ExtDemo {



  static void Main() {

    double val = 8.0;

    string str = "Alpha Beta Gamma";

    // Call the Recip() extension method.

    Console.WriteLine("Reciprocal of {0} is {1}",

                      val, val.Reciprocal());

www.freepdf-books.com



584

 

P a r t   I :  



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

    // Call the RevCase() extension method.

    Console.WriteLine(str + " after reversing case is " +

                      str.RevCase());

    // Use AbsDivideBy();

    Console.WriteLine("Result of val.AbsDivideBy(-2): " +

                      val.AbsDivideBy(-2));

  }


}

The output is shown here:

Reciprocal of 8 is 0.125

Alpha Beta Gamma after reversing case is aLPHA bETA gAMMA

Result of val.AbsDivideBy(-2): 4

In the program, notice that each extension method is contained in a static class called 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   732   733   734   735   736   737   738   739   ...   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