C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet628/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   624   625   626   627   628   629   630   631   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

T

 when a 


Gen

 object is instantiated. 

For example, if type 

string

 is specified for 



T

, then in that instance, 



ob

 will be of type 



string

.

www.freepdf-books.com




498

 

P a r t   I :  



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

Now consider 



Gen

’s constructor:

public Gen(T o) {

  ob = o;

}

Notice that its parameter, 



o

, is of type 



T

. This means that the actual type of 



o

 is determined 

by the type bound to 

T

 when a 


Gen

 object is created. Also, because both the parameter 



o

and the instance variable 



ob

 are of type 



T

, they will both be of the same actual type when a 



Gen

 object is created.

The type parameter 

T

 can also be used to specify the return type of a method, as is the 

case with the 

GetOb( )

 method, shown here:

public T GetOb() {

  return ob;

}

Because


ob

 is also of type 



T

, its type is compatible with the return type specified by 



GetOb( )

.

The



ShowType( ) 

method displays the type of 



T

 by passing 



T

 to the 


typeof

 operator. 

Because a real type will be substituted for 

T

 when an object of type 



Gen

 is created, 



typeof

will obtain type information about the actual type.

The

GenericsDemo

class demonstrates the generic 



Gen

 class. It first creates a version of 



Gen

 for type 



int

, as shown here:

Gen iOb;

Look closely at this declaration. First, notice that the type 



int

 is specified within the angle 

brackets after 

Gen

. In this case, 



int

 is a 


type argument

 that is bound to 



Gen

’s type parameter, 



T

. This creates a version of 



Gen

 in which all uses of 



T

 are replaced by 



int

. Thus, for this 

declaration,

ob

 is of type 



int

, and the return type of 



GetOb( )

 is of type 



int

.

The next line assigns to 



iOb

 a reference to an instance of an 



int

 version of the 



Gen

 class:


iOb = new Gen(102);

Notice that when the 



Gen

 constructor is called, the type argument 



int

 is also specified. This 

is necessary because the type of the variable (in this case 

iOb

) to which the reference is being 

assigned is of type 

Gen

. Thus, the reference returned by 



new

 must also be of type 



Gen

. If it isn’t, a compile-time error will result. For example, the following assignment 

will cause a compile-time error:

iOb = new Gen(118.12); // Error!

Because

iOb

 is of type 



Gen

, it can’t be used to refer to an object of 



Gen

. This 


type checking is one of the main benefits of generics because it ensures type safety.

The program then displays the type of 



ob

 within 


iOb

, which is 



System.Int32

. This is the 

.NET structure that corresponds to 

int

. Next, the program obtains the value of 



ob

 by use of 

the following line:

int v = iOb.GetOb();

Because the return type of 

GetOb( )

 is 


T

, which was replaced by 



int

 when 


iOb

 was declared, 

the return type of 

GetOb( )

 is also 



int

. Thus, this value can be assigned to an 



int

 variable.

www.freepdf-books.com



PART I

C h a p t e r   1 8 :  

G e n e r i c s  

499


PART IPART I

Next,


GenericsDemo

 declares an object of type 



Gen

:

Gen strOb = new Gen("Generics add power.");



Because the type argument is 

string

,

string

 is substituted for 

T

 inside 


Gen

. This creates a 



string

 version of 



Gen

, as the remaining lines in the program demonstrate.

Before moving on, a few terms need to be defined. When you specify a type argument 

such as 


int

 or 


string

 for 


Gen

, you are creating what is referred to in C# as a 



closed constructed

type.

 Thus, 


Gen

 is a closed constructed type. In essence, a generic type, such as 



Gen

, is an abstraction. It is only after a specific version, such as 



Gen

, has been 

constructed that a concrete type has been created. In C# terminology, a construct such as 

Gen

 is called an 



open constructed type,

 because the type parameter 



T

 (rather than an 

actual type, such as 

int

) is specified.

More generally, C# defines the concepts of an 

open type

 and a 


closed type.

 An open type is 

a type parameter or any generic type whose type argument is (or involves) a type parameter. 

Any type that is not an open type is a closed type. A 



constructed type

 is a generic type for 

which all type arguments have been supplied. If those type arguments are all closed types, 

then it is a closed constructed type. If one or more of those type arguments are open types, 

it is an open constructed type.

Generic Types Differ Based on Their Type Arguments

A key point to understand about generic types is that a reference of one specific version of a 

generic type is not type-compatible with another version of the same generic type. For example, 

assuming the program just shown, the following line of code is in error and will not compile:

iOb = strOb; // Wrong!

Even though both 



iOb

 and 


strOb

are of type 



Gen

, they are references to different types 

because their type arguments differ.

How Generics Improve Type Safety

At this point, you might be asking yourself the following question. Given that the same 

functionality found in the generic 

Gen

 class can be achieved without generics, by simply 

specifying

object

 as the data type and employing the proper casts, what is the benefit of 

making

Gen

 generic? The answer is that generics automatically ensure the type safety of all 

operations involving 

Gen

. In the process, generics eliminate the need for you to use casts 

and type-check code by hand.

To understand the benefits of generics, first consider the following program that creates 

a non-generic equivalent of 

Gen

:

// NonGen is functionally equivalent to Gen but does not use generics.



using System;

class NonGen {

  object ob; // ob is now of type object

  // Pass the constructor a reference of type object.

  public NonGen(object o) {

www.freepdf-books.com




500

 

P a r t   I :  



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

    ob = o;

  }

  // Return type object.



  public object GetOb() {

    return ob;

  }

  // Show type of ob.



  public void ShowType() {

    Console.WriteLine("Type of ob is " + ob.GetType());

  }

}

// Demonstrate the non-generic class.



class NonGenDemo {

  static void Main() {

    NonGen iOb;

    // Create NonGen object.

    iOb = new NonGen(102);

    // Show the type of data stored in iOb.

    iOb.ShowType();

    // Get the value in iOb.

    // This time, a cast is necessary.

    int v = (int) iOb.GetOb();

    Console.WriteLine("value: " + v);

    Console.WriteLine();

    // Create another NonGen object and store a string in it.

    NonGen strOb = new NonGen("Non-Generics Test");

    // Show the type of data stored in strOb.

    strOb.ShowType();

    // Get the value of strOb.

    // Again, notice that a cast is necessary.

    String str = (string) strOb.GetOb();

    Console.WriteLine("value: " + str);

    // This compiles, but is conceptually wrong!

    iOb = strOb;

    // The following line results in a runtime exception.

    // v = (int) iOb.GetOb(); // runtime error!

  }

}

This program produces the following output:



Type of ob is System.Int32

value: 102

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   624   625   626   627   628   629   630   631   ...   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