Join David Gassner for an in-depth discussion in this video Using built-in data types, part of C# Essential Training.
- In C Sharp, when we store data we have to say what type of data we're storing. Each variable has a data type. A data type defines what you can put into the variable. A variable can be numeric, it can be a string value, it can be a logical value like true false. But it can't be all of the above, you have to choose. And it's up to you as the programmer to decide what each variable can contain. We say that C# is a strongly typed language.
And an object is an instance of a class or a structure. In C#, each and every data type is represented as either a class or a structure. That's different from some other languages. For example in Java, you distinguish between primitive data types such as numeric values and boolean values, and reference types which are instances of classes or structures. In C#, everything is an instance of a class or a structure.
And therefore, every variable is an instance of some class or structure that has properties and methods. And that extends to the simplest of data types, such as boolean and integer values. As I've mentioned previously, it doesn't matter for the purpose of instantiating something, whether it starts as a class or a structure, but there are differences between the two. Structures are what we call value types.
This is similar in concept to Java's primitives, in the sense that if you start with one variable, and then you declare and assign a second variable from the first, you're making a copy. If you now change the value of v2, it won't affect v1, if the data type started off as a structure, and that's true of integers. On the other hand, classes are reference types. The object class is a class, not a structure.
And so, if I declare an object, which I named here o1, and then I declare a second object and I reference o1, now if I change properties of o2 I will also affect o1. Because in this case both variables are pointing to, or referencing, a single object in memory. In general, structures are value types and classes are reference types. But there are some exceptions, and I'll talk about that in a moment.
Here are some of the most commonly used structures you'll use in your C# programming. The data types that represent numeric and logical values in C# are defined as structures that is as value types. For example, booleans, integers, floating-point values, and decimals are all defined as structures in the .NET framework and the windows runtime, and represented in C# that way as well. Here are some examples of structures you'll use very frequently.
Each data type is represented as its native type and as a C# alias. The first column here is the alias. It's a key word in C# that represents an underlying class or structure, in this case structures. For the first one, the C# alias or type is bool and the equivalent .NET type is Boolean. Because this is a structure it starts with an uppercase "B", but the C# alias starts with a lowercase character.
Boolean variables only have two possible values, false and true. There is no numeric equivalent to these. You refer to Boolean values with the keywords true and false. A character type represents a single numeric or alphabetical character. The C# alias is char, c-h-a-r, all lowercase, and the .NET type is the same, but with an uppercase initial character. All .NET data types are members of the system namespace.
So the fully qualified structure name would be System.Boolean, or System.Char, but in your code you'll typically refer to these with a C# alias, bool or char, all lowercase. Here are the integer types, and there are eight of them. Starting with the smallest value "byte", and finishing with the longest "long". For each of the four basic types there's a signed and an unsigned version. The signed version can have both negative and positive values, while the unsigned version only has zero or positive values.
The ranges change depending on whether you're working with signed or unsigned values. Byte for example can start with zero and go to 255. An assigned byte, or sbyte, can start at -128 and go to positive 127. And each of these types then shows the potential range. For most of the programming I use in this course, I start off with an "int" value. That gives you a very broad range, or a "uint" if I need negative values.
And I typically only go to bytes and shorts if I have a specific need for it. Floating-point and decimal types include float, double, and decimal. The most commonly used of these is the double value, but there are issues you'll run into with double values when you're doing precise mathematical operations. For example, if you're working with currency values, it's strongly recommended that you use the decimal data type for these. Decimal is a 128-bit, high precision type.
It will accurately reflect and calculate currency values in situations where doubles and floats might not be reliable. And finally that brings us to strings. A string is exactly what it says, a set of characters, and as you see here string data types have a very long potential maximum length. But that's only theoretical actually, because you really need to pay attention to available memory. For example, if you're working in a mobile app on a phone device, you don't have that much memory available so creating a string that's that long might cause the device to run out of memory.
Also, string is kind of unique. It's defined as a class, not a structure, but in actual use it behaves more like a structure. A string variable is immutable, that is, it can't really be changed after it's been created. But the C# compiler has some very special ways of dealing with strings that make it look like you can modify it after it's been created, and compare strings to each other in ways that don't really make sense for most classes.
I'll talk about strings a lot more later, but for now it's important to know that string, along with all these others, is a built in data type in C#, and it's represented like all the others by a .NET data type. And if you're working in window's store apps with C#, you'll use the same identifiers to refer to the string, integer types, logical types, and so on.
- Declaring and initializing variables with a variety of data types
- Exploring operators, expressions, constants, and enumerations
- Controlling flow with conditional code and loops
- Handling exceptions
- Managing data collections
- Creating custom classes
- Organizing classes by namespace
- Understanding inheritance