Sams Teach Yourself .Net in 21 Days
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You
- Introduction
- Week 1: At a Glance
- Day 1. Introduction to the Microsoft .NET Framework
- Day 2. Introduction to Visual Studio .NET
- Day 3. Writing Windows Forms Applications
- Day 4. Deploying Windows Forms Applications
- Day 5. Writing ASP.NET Applications
- Day 6. Deploying ASP.NET Applications
- Day 7. Exceptions, Debugging, and Tracing
- Week 1. In Review
- Week 2: At a Glance
- Day 8. Core Language Concepts in Visual Basic .NET and C#
- Languages in the .NET Framework
- Understanding CLS Data Types in Visual Basic .NET and C#
- Declaring Variables in Visual Basic .NET and C
- Understanding Operators
- Using Decision Structures
- Summary
- Q&A
- Exercises
- Quiz
- Day 9. Using Namespaces in .NET
- Day 10. Accessing Data with ADO.NET
- Day 11. Understanding Visual Database Tools
- Day 12. Accessing XML in .NET
- Day 13. XML Web Services in .NET
- Day 14. Components and .NET
- Week 2. In Review
- Week 3: At a Glance
- Day 15. Writing International Applications
- Day 16. Using Macros in Visual Studio .NET
- Day 17. Automating Visual Studio .NET
- Day 18. Using Crystal Reports
- Day 19. Understanding Microsoft Application Center Test
- Day 20. Using Visual SourceSafe
- Day 21. Object Role Modeling with Visio
- Week 3. In Review
Understanding CLS Data Types in Visual Basic .NET and C#
When working with data, such as setting or retrieving values from a form, you must normally take that information and either manipulate it or store it temporarily before you persist it to a database or a file.
In C# and Visual Basic .NET, you use variables to store information. Each variable that you declare is of a certain type, which is defined by the rules of the common type system (CTS). The CTS guarantees type safety between different languages, which was unheard of before .NET. That means when you use a String data type in C#, it's the same String data type that you use in Visual Basic .NET and COBOL .NET.
When I talk about different .NET languages, I mean that a language is considered a .NET language because it follows the rules of the Common Language Specification (CLS). The CTS exists in the CLS, which ensures type safety across all languages that have a .NET suffix. The allowable data types, their language-specific syntax, their CLS type, and their value ranges are listed in Table 8.1.
Table 8.1. Data Types in .NET
|
Visual Basic .NET |
C# |
CLS Type |
Bytes |
Value |
|
Boolean |
bool |
System.Boolean |
4 |
True or False. |
|
Byte |
byte |
System.Byte |
1 |
0 to 255. |
|
Char |
char |
System.Char |
2 |
0 to 65,535. |
|
Date |
DateTime |
System.DateTime |
8 |
January 1, 1 to December 31, 9999. |
|
Decimal |
decimal |
System.Decimal |
12 |
+/- 79,228,162,514,264,337,593,950,335 with no decimal point. +/- 7.9228162512264337593543950335 with 28 places to the right of the decimal point. The smallest nonzero number would be a 1 in the 28th position to the right of the decimal point. |
|
Double |
double |
System.Double |
8 |
-1.797693134862231E308 to -4.94065645841247 for negative values to 4.94065645841247 to 1.797693134862231E308 for positive values. |
|
Integer |
int |
System.Int32 |
4 |
-2,147,483,648 to 2,147,483,648. |
|
Long |
long |
System.Int64 |
8 |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. |
|
Short |
short |
System.Int16 |
2 |
-32,768 to 32,767. |
|
Object |
object |
System.Object |
4 |
An object can hold any variable type. |
|
Single |
float |
System.Single |
4 |
-3.402823E38 to -1.401298E-45 for negative values to 1.401298E-45 to 3.402823E38 for positive values. |
|
String |
string |
System.String |
10+(2*Length) |
0 to 2 billion Unicode characters (approximately). |
|
User-defined type (structure) |
struct |
System.ValueType |
Sum of the size of its members |
Each member of the structure has a range determined by its data type and independent of the ranges of the other members. |
Declaring Variables in Visual Basic .NET and C | Next Section

Account Sign In
View your cart