C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet715/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   711   712   713   714   715   716   717   718   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Count

,

Max

, and 

Min

.

These are given the values 10, 100, and 0, respectively. These properties can be referred to by 



name by other code. Notice that an anonymous type uses object initializers to initialize fields 

and properties. As explained in Chapter 8, object initializers provide a way to initialize an 

object without explicitly invoking a constructor. This is necessary in the case of anonymous 

types because there is no way to explicitly call a constructor. (Recall that constructors have 

the same name as their class. In the case of an anonymous class, there is no name. So, how 

would you invoke the constructor?)

Because an anonymous type has no name, you must use an implicitly typed variable to 

refer to it. This lets the compiler infer the proper type. For example,

var myOb = new { Count = 10, Max = 100, Min = 0 }

creates a variable called 



myOb

 that is assigned a reference to the object created by the 

anonymous type expression. This means that the following statements are legal:

Console.WriteLine("Count is " + myOb.Count);

if(i <= myOb.Max && i >= myOb.Min) // ...

www.freepdf-books.com




570

 

P a r t   I :  



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

Remember, when an anonymous type is created, the identifiers that you specify become 

read-only public properties. Thus, they can be used by other parts of your code.

Although the term 



anonymous type

 is used, it’s not quite completely true! The type is 

anonymous relative to you, the programmer. However, the compiler does give it an internal 

name. Thus, anonymous types do not violate C#‘s strong type checking rules.

To fully understand the value of anonymous types, consider this rewrite of the previous 

program that demonstrated 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   711   712   713   714   715   716   717   718   ...   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