Understanding C# Types
In Hour 1, “The .NET Framework and C#,” you were introduced to the fundamentals of the .NET Framework and C#, including the framework class library, the common language runtime, and the idea of automatic memory management. You briefly learned about namespaces and types and then moved on to statements, expressions, variables, constants, identifiers, and keywords. From those humble beginnings, you then built a simple C# application and learned about debugging in Hour 2, “Introducing Visual Studio.”
Building on what you have already learned, this hour introduces you to the predefined types offered by C# and the different operations that you can perform using them. You then learn about value and reference types. After that, you see nullable types and learn about type conversion.
At the end of this hour, you should have a thorough understanding of the C# types, including the difference between value, reference, and nullable types. You will also have written some more advanced applications that can store and manipulate simple data.
An Overview of Types
C# is both a type-safe and statically typed language. Being statically typed requires you to inform the compiler of the data type for any variable you create. In return, the compiler guarantees that you can only store a compatible data type in that variable, making it type safe. This combination helps prevent common programming errors, leading to a more stable and secure application.
Types are divided into three main categories:
- Value types
- Reference types
- Type parameters
Put simply, a value type is completely self-contained and copied “by value.” This means that variables of a value type directly contain their data, and it is not possible for operations on one to affect the other. Value types are further categorized into structures, enumerated types, and nullable types.
A reference type contains a reference to the actual data, meaning it is possible for two variables to reference the same object, allowing the possibility that operations on one will affect the other. Reference types are further categorized into classes, arrays, interfaces, and delegates.