In every programming language there is a compiler. The compiler is the part of the Visual Studio .NET Framework that interprets the code you write into a language the computer can understand. The compiler must understand 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 + "Fender" 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 halts execution and displays an exception (error) when it reaches the confusing line of code. (These two types of errors are discussed in detail in Hour 15, "Debugging Your Code.") Obviously, you can't add the word Fender to the number 659 because these two values are different types of data. In Visual C#, these two values are said to have two different data types. In Visual C#, constants, variables, and arrays must always be defined to hold a specific type of information. Determining Data TypeData typingthe act of defining a constant, variable, or array's data typecan be confusing. To Visual C#, a number is not simply a number. A number that contains a decimal value is different from a number that doesn't. Visual 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. Visual 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. Because of this limitation, you must give careful consideration to the type of data you plan to store in a constant, variable, or array at the time you define it. Table 11.1 lists the Visual C# data types and the range of values each one can contain.
Visual C# supports unsigned data types for short, int, and long (the types prefaced 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). Did you Know? Tips for Determining Data Type The list of data types might seem daunting at first, but you can follow some general guidelines for choosing among them. As you become more familiar with the different types, you'll be able to fine-tune your data type selection. Following are some helpful guidelines for using data types:
By the Way The Object data type requires special attention. If you define a variable or array as an Object data type, you can store just about any value you care to in it; Visual C# determines what data type to use when you set the variable's value. Several drawbacks exist to using Object data types. First, Object data types take up more memory than the other data types. In addition, Visual C# takes a little longer to perform calculations on Object data types. Unless you have a specific reason to do soand there are valid reasons, such as when you don't know the type of data to be stored ahead of timedon't use the Object data type. Instead, become familiar with the explicit data types and use them appropriately. Casting Data from One Data Type to AnotherUnder most circumstances, Visual C# won't allow you to move data of one type into a variable of another type. The process of changing a value's data type is known as casting. Visual C# supports two types of casting: implicit and explicit. Implicit conversions are done automatically by the compiler. These conversions guarantee that no data is lost in the conversion. For instance, you can set the value of a variable declared as double to the value of a variable declared as float without an explicit cast because there is no risk of losing data; the double data type holds a more precise value than does a float, and this type of cast is called a widening cast. Explicit casting is required when a potential exists for data loss or when converting a larger data type into a smaller data type (a narrowing cast). 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, Visual C# requires that these types of conversions be explicitly written using the cast operator. Table 11.2 lists some of the type conversions that can be done implicitly with no loss of information.
To explicitly convert data from one type to another, you use one of the methods of the Convert class. Table 11.3 lists these methods. The use of these methods is pretty straightforward: Pass the data to be cast as the parameter, and the method returns the value with the return type. For example, to place the value of a variable declared as double into a variable declared as single, you could use a statement such as the following: sngVariable = Convert.ToSingle(dblVariable);
|