C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet192/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   188   189   190   191   192   193   194   195   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

x

 of 


MyClass

 the value 10. This constructor is called by 



new

 when an object is created. For 

example, in the line

MyClass t1 = new MyClass();

the constructor 

MyClass( )

 is called on the 



t1

 object, giving 



t1.x

 the value 10. The same is 

true for 

t2

. After construction, 



t2.x

 has the value 10. Thus, the output from the program is

10 10

Parameterized Constructors

In the preceding example, a parameterless constructor was used. While this is fine for some 

situations, most often you will need a constructor that accepts one or more parameters. 

Parameters are added to a constructor in the same way they are added to a method: just 

declare them inside the parentheses after the constructor’s name. For example, here 

MyClass

 is given a parameterized constructor:

// A parameterized constructor.

using System;

class MyClass {

  public int x;

  public MyClass(int i) {

    x = i;

  }

}

class ParmConsDemo {



  static void Main() {

    MyClass t1 = new MyClass(10);

    MyClass t2 = new MyClass(88);

    Console.WriteLine(t1.x + " " + t2.x);

  }

}

The output from this program is shown here:



10 88

In this version of the program, the 



MyClass( )

 constructor defines one parameter called 



i

,

which is used to initialize the instance variable, 



x

. Thus, when the line

MyClass t1 = new MyClass(10);

executes, the value 10 is passed to 



i

, which is then assigned to 



x

.

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   188   189   190   191   192   193   194   195   ...   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