The Microsoft .NET Framework provides a robust system of primitive types to represent data. Data primitives exist for the representation of integer numbers, floating-point numbers, Boolean values, characters, and strings. The type system enforces type-safety, so that implicit casts only occur when no loss of data is possible. Explicit conversions can be performed in situations that might cause a loss of data. The .NET data types contain built-in functionality that performs a variety of type related conversions and tasks.
Data types are used to store and represent data in your application. The .NET Framework provides an extensive system of types that allow you to store, manipulate, and pass values between members of your application. The .NET languages are strongly typed.
This means that objects of one type cannot be freely exchanged with objects of a different type. All parameters in method and property calls must be of the appropriate type, or the call will fail. Implicit and explicit conversions allow you to convert data types when necessary. Such conversions are possible because all types in the .NET Framework derive from Object, the base type of all classes and structures.
The .NET Data Types
The .NET data types are the types you use to store your data. They are value types and can be broken down into subcategories: Integer types, Floating-point types, the Boolean type, and the Char type. Two built-in reference types that are an integral part of your application will also be discussed: the String type and the Object type.
Integer Types
The .NET Framework provides a variety of Integer types. Table 3.1 summarizes these types and lists their corresponding Microsoft Visual Basic .NET and Microsoft Visual C# types.
The System.Single type is appropriate for floating-point calculations that require a lower degree of precision. It provides seven significant digits of precision. For greater levels of precision, you can use the System.Double type. This provides a much greater level of precision as well as the ability to handle vastly larger values. The System.Decimal type is specifically designed to facilitate financial calculations and is an ultra-high precision variable. Although it cannot hold values as great as System.Double, it has a much higher level of precision, providing 28 significant digits.
Non-Numeric Types
Four additional types that do not represent numbers are discussed in this section: System.Boolean, System.Char, System.String, and System.Object.
System.Boolean
The System.Boolean type is used to represent a value that is either true or false. It is called Boolean in Visual Basic .NET, and bool in Visual C#. The values that are valid for Boolean variables are True and False (Visual Basic .NET) or true and false (Visual C#).
System.Char
The System.Char type represents a single instance of a 16-bit Unicode character. It is called char in Visual C# and Char in Visual Basic .NET.
System.String
The System.String type is a reference type, and it represents a series of Char data types. In everyday terms, a string can represent a word, a paragraph, a key value, or any other string of characters. In Visual Basic .NET this type is called String, and in Visual C# it is called string.
System.Object
The Object type is the supertype of all types in the .NET Framework. Every type, whether value type or reference type, derives from System.Object. In Visual Basic .NET it is called Object, and in Visual C# it is called object.
If an object of a particular type is stored in an Object variable, it must be explicitly converted back to that type to access any of its inherent functionality.
Converting Types
At times, you will need to convert data from one type to another. Data can be converted in two ways: implicitly, which means the conversion is performed automatically, and explicitly, which means you must specifically ask for the conversion to be performed.
Implicit Conversions
Implicit conversions between types are automatically performed whenever the conversion can be performed without the loss of data.
Using Data Type Functionality
All of the data types have some kind of built-in functionality. At the very least, they all support four methods:
- Equals. Determines whether two instances are equal
- GetHashCode. Serves as a hash function for a particular type
- GetType. Returns the type object for the current instance
- ToString. Returns a human-readable form of the object
Boxing
The secret is called boxing. Boxing is the implicit conversion of value types to reference types. All classes and types derive from Object, and each of these four methods is a method of the Object class. Because each class derives from Object, they can be implicitly converted to that type. When you call one of these methods, the run time creates a temporary reference for your stack and allows you to treat it as a reference type.
Unboxing is the conversion of a boxed variable back to a value type. To unbox a variable, you must perform an explicit cast on the object to convert it to the appropriate type. Only objects that have been boxed can be unboxed.
Data Type Member Methods
Data types also have other methods that do not derive from Object. These methods usually involve functionality specific to the type. In Chapter 2, you were introduced to some of the comparison functions of the Char data type. Although the sheer scope of these methods is too great to be adequately dealt with in this text, some of the more generally useful functions will be discussed.
Parse
All of the value data types implement a Parse method. Parse is used to create a numeric value from a string. The Parse method is extremely useful when developing user interfaces. Controls that accept user input, such as TextBox, do so in the form of a string. The Parse method can be used to convert that string to usable data. Note that if a string cannot be read as a numeric value, an error will result. In all implementations, Parse is a static (Shared) method, and must be called from the type object rather than from an instance.
To convert a string to a numeric data type
Call the Parse method of that data type on that method.
String Functions
String manipulations are keenly important for many applications—converting strings to display formats, for instance, or extracting substrings from existing strings. The String class exposes a variety of member methods for the manipulation of strings.
Related post
DAY 1 MICROSOFT DOT NET FRAME WORK
DAY 2 MICROSOFT DOT NET BASE CLASS LIBRARY
DAY 3 MICROSOFT DOT NET CLASSES AND STRECTURES
DAY 4 METHODS IN FRAME WORK
DAY 5 INPUT VALIDATIONS IN DOT NET PART ONE
DAY 6 INPUT VALIDATIONS IN DOT NET PART TWO
DAY 7 DATA TYPES IN DOT NET
DAY 8 DATA TYPES IN DOT NET PART TWO
DAY 9 IMPLEMENTING PROPERTIES IN DOT NET
DAY 10 DELEGATES AND EVENTS
No comments:
Post a Comment