5.2. Data Types and Variables

At first when you think about data, you may not realize that each piece of data has a data type. You may think that a computer would store the words Hello World in exactly the same way as today's date or the number 6. However, to be able to effectively work with data, many programming languages have different data types, where each data type is constrained to a specific type of information. Out of the box, the .NET Framework comes with a long list of data types that allows you to work with numbers (like Integer, Short, and Double), text strings (Char and String), dates (DateTime), true/false constructs (the Boolean), and more. A list with the most common types is described later in this section.

For each major type of data there is a special data type. To work with that type, you can store it in a variable that you need to declare first using the required data type. In VB.NET you use Dim myVariable As DataType while in C# you use DataType myVariable. The following example shows you how to declare two variables: an Integer (int in C#) to hold a number and a String (string in C#) to hold a piece of text:

VB.NET

' Declare a variable of type Integer to hold medium sized whole numbers.
Dim distanceInMiles As Integer

' Declare a variable to hold some text like a first name.
Dim firstName As String

C#

// Declare a variable of type int to hold medium sized whole numbers. int distanceInMiles; // Declare a variable to hold some text like a first name. string ...

Get Beginning ASP.NET 3.5: In C# and VB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.