15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Predefined Data Types ❘ 31<br />

...<br />

public static void Main()<br />

{<br />

int j = 30;<br />

Console.WriteLine(j);<br />

Console.WriteLine(ScopeTest2.j);<br />

}<br />

...<br />

If you were accessing an instance field (a field that belongs to a specific instance of the class), you would<br />

need to use the this keyword instead.<br />

Constants<br />

As the name implies, a constant is a variable whose value cannot be changed throughout its lifetime.<br />

Prefixing a variable with the const keyword when it is declared <strong>and</strong> initialized designates that variable as a<br />

constant:<br />

const int a = 100; // This value cannot be changed.<br />

Constants have the following characteristics:<br />

➤<br />

➤<br />

➤<br />

They must be initialized when they are declared, <strong>and</strong> after a value has been assigned, it can never be<br />

overwritten.<br />

The value of a constant must be computable at compile time. Therefore, you can’t initialize a constant<br />

with a value taken from a variable. If you need to do this, you will need to use a read-only field (this is<br />

explained in Chapter 3).<br />

Constants are always implicitly static. However, notice that you don’t have to (<strong>and</strong>, in fact, are not<br />

permitted to) include the static modifier in the constant declaration.<br />

At least three advantages exist for using constants in your programs:<br />

➤<br />

➤<br />

➤<br />

Constants make your programs easier to read by replacing magic numbers <strong>and</strong> strings with readable<br />

names whose values are easy to underst<strong>and</strong>.<br />

Constants make your programs easier to modify. For example, assume that you have a SalesTax<br />

constant in one of your <strong>C#</strong> programs, <strong>and</strong> that constant is assigned a value of 6 percent. If the sales<br />

tax rate changes at a later point in time, you can modify the behavior of all tax calculations simply by<br />

assigning a new value to the constant; you don’t have to hunt through your code for the value .06 <strong>and</strong><br />

change each one, hoping that you will find all of them.<br />

Constants help prevent mistakes in your programs. If you attempt to assign another value to a constant<br />

somewhere in your program other than at the point where the constant is declared, the compiler<br />

will flag the error.<br />

Predefined daTa TyPes<br />

Now that you have seen how to declare variables <strong>and</strong> constants, let’s take a closer look at the data types<br />

available in <strong>C#</strong>. As you will see, <strong>C#</strong> is much stricter about the types available <strong>and</strong> their definitions than<br />

some other languages are.<br />

Value Types <strong>and</strong> reference Types<br />

Before examining the data types in <strong>C#</strong>, it is important to underst<strong>and</strong> that <strong>C#</strong> distinguishes between two<br />

categories of data type:<br />

➤<br />

➤<br />

Value types<br />

Reference types<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!