C# 0 The Complete Reference


y  coordinate to the same value as the  x



Download 4,07 Mb.
Pdf ko'rish
bet272/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   268   269   270   271   272   273   274   275   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

y

 coordinate to the same value as the 



x

coordinate. Of course, it is wise to use such “default arguments” carefully because 

their misuse could easily confuse users of your classes.

Object Initializers

C# 3.0 added a new feature called 



object initializers

 that provides another way to create an 

object and initialize its fields and properties. (See Chapter 10 for a discussion of properties.) 

Using object initializers, you do not call a class’ constructor in the normal way. Rather, you 

specify the names of the fields and/or properties to be initialized, giving each an initial 

value. Thus, the object initializer syntax provides an alternative to explicitly invoking a 

class’ constructor. The primary use of the object initializer syntax is with anonymous types 

created in a LINQ expression. (Anonymous types and LINQ are described in Chapter 19.) 

However, because the object initializers can be used (and occasionally are used) with a 

named class, the fundamentals of object initialization are introduced here.

Let’s begin with a simple example:

// A simple demonstration that uses object initializers.

using System;

class MyClass {

  public int Count;

  public string Str;

}

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);

  }

}

This produces the following output:



100 Testing

As the output shows, the value of 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   268   269   270   271   272   273   274   275   ...   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