Working with Variables in C 2.0


Working with Variables in C# 2.0

My favorite analogy for explaining variables is the "bucket" analogy. Think of a variable as a bucket. Into that bucket, you can place data. Some buckets don't care what kind of data you place in them, and other buckets have specific requirements on the type of data you can place in them. You can move data from one bucket to another. Unfortunately, the bucket analogy gets a little confusing when you take into account that one bucket can contain a little note inside that reads "see Bucket B for actual data" (you'll read about reference types shortly in the section "Value Types vs. Reference Types").

To declare a variable in C#, you can use the following syntax:

type variable_name; 


You can initialize a variable on the same line:

type variable_name = initialization expression 


where type is a .NET type. The next section lists some of the core .NET types.

Common .NET Types

Table 1.1 shows some of the basic .NET data types. As you will see in later chapters, this is just the beginning. When you start using classes, the variety of types available to you will be virtually unlimited.

Table 1.1. Core .NET Data Types

Data Type

Description

System.Boolean

Provides a way to store true/false data.

System.Byte

Represents a single byte of data.

System.Char

A single character. Unlike other languages, this character is a 2-byte Unicode character.

System.Decimal

A decimal value with 28 to 29 significant digits in the range ±1.0 x 1028 to ±7.9 x 1028.

System.Double

A double-precision value that represents a 64-bit floating-point value with 15 to 16 significant digits in the range ±5.0 x 10324 to ±1.7 x 10308.

System.Single

A single-precision value that represents a 32-bit floating point number in the range ±1.5 x 1045 to ±3.4 x 1038.

System.Int32

Represents a 32-bit signed integer in the range 2,147,483,648 to 2,147,483,647.

System.Int64

Represents a 64-bit signed integer in the range 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

System.SByte

A signed 8-bit integer.

System.Int16

A signed 16-bit integer.

System.UInt32

An unsigned 32-bit integer.

System.UInt64

An unsigned 64-bit integer.

System.UInt16

An unsigned 16-bit integer.

System.String

An arbitrary-length character string that can contain Unicode strings.


If you aren't familiar with .NET or C#, you may be wondering what the "System" is in the data types listed in Table 1.1. .NET organizes all types into namespaces. A namespace is a logical container that provides name distinction for data types. These core data types all exist in the "System" namespace. You'll see more namespaces throughout the book as you learn about more specific aspects of .NET.

Type Shortcuts

C# provides you with some shortcuts to make declaring some of the core data types easier. These shortcuts are simple one-word lowercase aliases that, when compiled, will still represent a core .NET type. Table 1.2 lists some data type shortcuts and their corresponding .NET types.

Table 1.2. C# Aliases for .NET Data Types

Shortcut

.NET Type

bool

System.Boolean

byte

System.Byte

char

System.Char

decimal

System.Decimal

double

System.Double

float

System.Single

int

System.Int32

long

System.Int64

sbyte

System.SByte

short

System.Int16

uint

System.UInt32

ulong

System.UInt64

ushort

System.UInt16


Value Types vs. Reference Types

Up to this point, this chapter has just been illustrating data types in one category. Earlier in the chapter, I mentioned a "bucket" analogy where data in one bucket could actually refer to data contained in some other bucket. This is actually the core point to illustrate the difference between value types and reference types.

A value type is a type whose data is contained with the variable on the stack. Value types are generally fast and lightweight because they reside on the stack (you will read about the exceptions in Chapter 16, "Optimizing your .NET 2.0 Code").

A reference type is a type whose data does not reside on the stack, but instead resides on the heap. When the data contained in a reference type is accessed, the contents of the variable are examined on the stack. That data then references (or points to, for those of you with traditional C and C++ experience) the actual data contained in the heap. Reference types are generally larger and slower than value types. Learning when to use a reference type and when to use a value type is something that comes with practice and experience.

Your code often needs to pass very large objects as parameters to methods. If these large parameters were passed on the stack as value types, the performance of the application would degrade horribly. Using reference types allows your code to pass a "reference" to the large object rather than the large object itself. Value types allow your code to pass small data in an optimized way directly on the stack.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net