In any programming language, it's critical that the compiler, the part of the Visual Studio framework that interprets the code you write into a language the computer can understand, fully understands the type of data you're manipulating in code. For example, if you asked the compiler to add the following values, it would get confused:
659 / "Dog"
When the compiler gets confused, it either refuses to compile the code (which is the preferred situation because you can address the problem before your users run the application), or it will halt execution and display an exception (error) when it reaches the confusing line of code. (These two types of errors are discussed in detail in Hour 16, “Debugging Your Code.”) Obviously, you can't subtract 659 by the word “Dog”; these two values are different types of data. In C#, these two values are said to have two different data types. In C#, constants, variables, and arrays must always be defined to hold a specific type of information. |
Data typing—the act of defining a constant, a variable, or an array's data type—can be confusing. To C#, a number is not a number. A number that contains a decimal value is different from a number that does not. C# can perform arithmetic on numbers of different data types, but you can't store data of one type in a variable with an incompatible type. Because of this limitation, you must give careful consideration to the type of data you plan to store in a constant, a variable, or an array at the time you define it. C# supports two categories of data types: value types and reference types. The main difference between these two types is how their values are stored in memory. As you continue to create more complex applications, this difference may have an impact on your programming. For this book, however, this distinction is minimal. Table 12.1 lists the C# data types and the range of values they can contain. |
Data Type—Reference | Value Range |
---|---|
string | 0 to approximately 2 billion characters |
object | Any type can be stored in a variable type Object |
C# supports unsigned data types for short, int, and long (the types prefaces with u, such as uint). Because negative numbers are excluded (there is no sign) this has the effect of doubling the positive values for a short, an int, or a long. Signed data types are preferable and should be used unless you have a very good reason for doing otherwise (such as declaring a variable that will never hold a negative value).
Explicit casting is required when a potential exists for data loss or when converting a larger data type into a smaller data type. If you tried to place a value in a variable when the value was higher than the variable's supported data type, some data would be lost. Therefore, C# requires that these types of conversions be explicitly written using the cast operator. For instance, you can set the value of a variable declared as short to the value of a variable declared as integer using the following syntax:
short MyShortInterger; int MyInteger = 1000; MyShortInterger = (short) MyInteger;
Notice here that 1000 would fit in a short, so data wouldn't actually be lost if no explicit cast were performed. However, C# doesn't care; it's the potential for data loss that causes C# to require explicit casts.
Table 12.2 lists some of the type conversions that can be done implicitly with no loss of information.
3.135.202.203