This print for content only-size & color not accurate 7" X 9-1/4" / casebound / malloy


// Read-only fields can be assigned in ctors



Download 21,46 Mb.
Pdf ko'rish
bet370/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   366   367   368   369   370   371   372   373   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Read-only fields can be assigned in ctors,

// but nowhere else.

public readonly double PI;

public MyMathClass ()

{

PI = 3.14;



}

}

Again, any attempt to make assignments to a field marked readonly outside the scope of a con-



structor results in a compiler error:

class MyMathClass

{

public readonly double PI;



public MyMathClass ()

{

PI = 3.14;



}

// Error!

public void ChangePI()

{ PI = 3.14444;}

}

C H A P T E R   5  



D E F I N I N G   E N C A P S U L AT E D   C L A S S  T Y P E S



174

8849CH05.qxd  10/22/07  1:34 PM  Page 174




Static Read-Only Fields

Unlike a constant field, read-only fields are not implicitly static. Thus, if you wish to expose PI from

the class level, you must explicitly make use of the static keyword. If you know the value of a static

read-only field at compile time, the initial assignment looks very similar to that of a constant:

class MyMathClass

{

public static readonly double PI = 3.14;



}

class Program

{

static void Main(string[] args)



{

Console.WriteLine("***** Fun with Const *****");

Console.WriteLine("The value of PI is: {0}", MyMathClass.PI);

Console.ReadLine();

}

}

However, if the value of a static read-only field is not known until runtime, you must make use



of a static constructor as described earlier in this chapter:

class MyMathClass

{

public static readonly double PI;



static MyMathClass()

{ PI = 3.14; }

}

Now that we have examined the role of constant data and read-only fields, we can return to the



Employee example and put the wraps on this chapter.



Source Code

The ConstData project is included under the Chapter 5 subdirectory.



Understanding Partial Types

Classes and structures can be defined with a type modifier named partial that allows you to define

a type across multiple *.cs files. Earlier versions of the language required all code for a given type

be defined within a single *.cs file. Given the fact that a production-level C# class may be hundreds

of lines of code (or more), this can end up being a mighty lengthy file indeed.

In these cases, it may be beneficial to partition a type’s implementation across numerous *.cs

files in order to separate code that is in some way more important from other aspects of the type

definition. For example, using the partial class modifier, you could place all of the Employee con-

structors and properties into a new file named Employee.Internals.cs:


Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   366   367   368   369   370   371   372   373   ...   1830




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