C# 0 The Complete Reference


Use Object Initializers with Properties



Download 4,07 Mb.
Pdf ko'rish
bet342/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   338   339   340   341   342   343   344   345   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

    Bu sahifa navigatsiya:
  • Count
Use Object Initializers with Properties

As discussed in Chapter 8, C# 3.0 adds a new feature called an 



object initializer

, which 


provides an alternative to explicitly calling a constructor when creating an object. When 

using object initializers, you specify initial values for the fields and/or properties that you 

want to initialize. Furthermore, the object initializer syntax is the same for both properties 

or fields. For example, here is the object initializer demonstration program from Chapter 8, 

reworked to show the use of object initializers with properties. Recall that the version 

shown in Chapter 8 used fields. The only difference between this version of the program 

and the one shown in Chapter 8 is that 

Count

 and 


Str

 have been converted from fields into 

properties. The object initializer syntax is unchanged.

// Use object initializers with properties.

using System;

class MyClass {

  // These are now properties.

  public int Count { get; set; }

  public string Str { get; set; }

}

class ObjInitDemo {



  static void Main() {

    // Construct a MyClass object by using object initializers.

    MyClass obj = new MyClass { Count = 100, Str = "Testing" };

    Console.WriteLine(obj.Count + " " + obj.Str);

  }

}

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   338   339   340   341   342   343   344   345   ...   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