C# 0 The Complete Reference


PART I C h a p t e r   1 8



Download 4,07 Mb.
Pdf ko'rish
bet655/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   651   652   653   654   655   656   657   658   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   1 8 :  

G e n e r i c s  

525


PART IPART I

    // Copy src to target, inserting e at idx in the process.

    for(int i=0, j=0; i < src.Length; i++, j++) {

      if(i == idx) {

        target[j] = e;

        j++;

      }

      target[j] = src[i];

    }

    return true;



  }

}

class GenMethDemo {



  static void Main() {

    int[] nums = { 1, 2, 3 };

    int[] nums2 = new int[4];

    // Display contents of nums.

    Console.Write("Contents of nums: ");

    foreach(int x in nums)

      Console.Write(x + " ");

    Console.WriteLine();

    // Operate on an int array.

    ArrayUtils.CopyInsert(99, 2, nums, nums2);

    // Display contents of nums2.

    Console.Write("Contents of nums2: ");

    foreach(int x in nums2)

      Console.Write(x + " ");

    Console.WriteLine();

    // Now, use copyInsert on an array of strings.

    string[] strs = { "Generics", "are", "powerful."};

    string[] strs2 = new string[4];

    // Display contents of strs.

    Console.Write("Contents of strs: ");

    foreach(string s in strs)

      Console.Write(s + " ");

    Console.WriteLine();

    // Insert into a string array.

    ArrayUtils.CopyInsert("in C#", 1, strs, strs2);

    // Display contents of strs2.

    Console.Write("Contents of strs2: ");

    foreach(string s in strs2)

      Console.Write(s + " ");

www.freepdf-books.com




526

 

P a r t   I :  



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

    Console.WriteLine();

    // This call is invalid because the first argument

    // is of type double, and the third and fourth arguments

    // have element types of int.

//    ArrayUtils.CopyInsert(0.01, 2, nums, nums2);

  }

}

The output from the program is shown here:



Contents of nums: 1 2 3

Contents of nums2: 1 2 99 3

Contents of strs: Generics are powerful.

Contents of strs2: Generics in C# are powerful.

Let’s examine 

CopyInsert( )

 closely. First, notice how it is declared by this line:

public static bool CopyInsert(T e, uint idx,

                                 T[] src, T[] target) {

The type parameter is declared 

after

 the method name, but 



before

 the parameter list. Also 

notice that 

CopyInsert( )

 is static, enabling it to be called independently of any object. 

Understand, though, that generic methods can be either static or non-static. There is no 

restriction in this regard.

Now, notice how 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   651   652   653   654   655   656   657   658   ...   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