Working with Variables in C# 2.0My 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 TypesTable 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.
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 ShortcutsC# 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.
Value Types vs. Reference TypesUp 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. |