C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet184/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   180   181   182   183   184   185   186   187   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

AreaPerPerson( )

 can be used 

in the 

WriteLine( )

 statement directly, as shown here:

Console.WriteLine("house has:\n  " +

                  house.Floors + " floors\n  " +

                  house.Occupants + " occupants\n  " +

                  house.Area + " total area\n  " +

                  house.AreaPerPerson() + " area per person");

In this case, when 



WriteLine( )

 is executed, 



house.AreaPerPerson( )

 is called automatically, 

and its value will be passed to 

WriteLine( )

. Furthermore, you can use a call to 



AreaPerPerson( )

whenever the area-per-person of a 



Building

 object is needed. For example, this statement 

compares the per-person areas of two buildings:

if(b1.AreaPerPerson() > b2.AreaPerPerson())

  Console.WriteLine("b1 has more space for each person");

Use Parameters

It is possible to pass one or more values to a method when the method is called. A value 

passed to a method is called an 

argument. 

Inside the method, the variable that receives 

the argument is called a 

formal parameter,

 or just 



parameter,

 for short. Parameters are 

declared inside the parentheses that follow the method’s name. The parameter declaration 

syntax is the same as that used for variables. The scope of a parameter is the body of its 

method. Aside from its special task of receiving an argument, it acts like any other local 

variable.

Here is a simple example that uses a parameter. Inside the 

ChkNum

 class, the method 



IsPrime( )

 returns 



true

 if the value that it is passed is prime. It returns 



false

 otherwise. 

Therefore, 

IsPrime( )

 has a return type of 



bool

.

// A simple example that uses a parameter.



using System;

class ChkNum {

  // Return true if x is prime.

  public bool IsPrime(int x) {

    if(x <= 1) return false;

    for(int i=2; i <= x/i; i++)

      if((x %i) == 0) return false;

    return true;

  }

}

class ParmDemo {



  static void Main() {

    ChkNum ob = new ChkNum();

    for(int i=2; i < 10; i++)

      if(ob.IsPrime(i)) Console.WriteLine(i + " is prime.");

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   180   181   182   183   184   185   186   187   ...   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