The course is part of this learning path
In this course, we look at how different types of data are stored using variables within a C# program. C# is a strongly typed language, meaning when you manipulate data in code, you must keep the data in variables that are specifically designed to hold that kind of data. For example, text is stored in a string data type and a letter in a char. There are over ten different numeric data types that vary in size and accuracy or precision of the data they can faithfully represent. We investigate some of the quirks in dealing with fractional numbers in a computer's binary environment. There are in-depth code examples for each of these topics to illustrate the discussed concepts and make you more familiar with C# programming in general.
This course builds upon the key concepts and examples covered in the Introduction to C# course. It includes guided demonstrations to give you practical knowledge of how to handle the concepts covered.
- Understand what variables are and how they're stored
- Learn about data types for storing and manipulating text values
- Learn about the various data types for storing and manipulating whole and fractional numbers
- Learn about variables for storing multiple values of the same data type
This course is intended for anyone who has a basic understanding of C# and now wants to build upon that knowledge.
This course carries on from our Introduction to C# course, so we suggest taking that one first if you haven't already done so.
Code examples used in demos on GitHub: https://github.com/cloudacademy/csharp-datatypes-variables
This little diversion into bits, bytes, and hexadecimal is related to the myName variable, the string data type, and how text and characters are stored in memory and variables. There are two data types in C# that can represent text. Let's start with char and then string, that we've already seen. Char is short for character and represents a single character or letter. A literal char value is a character enclosed in single quotes. Not only can a char be a letter, digit, and punctuation, but also non-printing special characters like tab and carriage return, or new line
A special character is proceeded by an escape character that tells the compiler to interpret whatever follows the escape character, in a different way. In C#, as with many other programming languages, the backslash is the escape character. A tab character is represented by \t. A string is multiple characters in a row, that is, chars strung together.
There is a theoretical limit to the size of a string, due to the .NET limit on the size of a single variable, which is two gigabytes. As we've seen, each character takes up two bytes. So the maximum string size is just over a billion characters.
A string is enclosed in double quotes, and like a char, can have special characters such as tab and new line characters, embedded within it. Using a single quotation mark within a string, like the word "I'm", isn't a problem, but double quotes with inner string need to be escaped with a backslash.
When a variable is declared as a string or a char, the compiler knows to treat the value as such. This means the value 69 is translated into a letter, according to the ASCII code table. ASCII stands for American Standard Code for Information Interchange. The ASCII table translates a number into a character, even non printing characters like tabs and backspace.
Getting back to the hexadecimal value 45, we can see that it stands for uppercase E. Going back to the memory address of the myName variable, we can see the 45 representing the uppercase E, then 6c, which is lowercase L, 6f for lowercase O, and 6e for lowercase N. There is one more issue to address here, which isn't vitally important at this stage of your learning, but definitely something to keep in the back of your mind.
I said a byte is represented by the hexadecimal value, say 45 relates to the character E, but you can see there are zero value bytes between the character bytes. A byte representing 256 values is fine, when you only have to deal with a 26 letter alphabet with upper and lower case, a handful of punctuation characters, and some other special characters. But if you want to display characters from all alphabets on the planet, then you better get a bigger number.
Two bytes combined, that is 16 bits, is called a word and can hold it up to 65,536 values, which is ample to display all characters. While it is convenient to think of bytes being equivalent to characters, if you are coming from an English or Latin alphabet, that's not what's going on underneath the hood. In most circumstances, you don't need to think about it, and you can just use the char and string data types in the same way, whatever the character set you're working with.
You may have come across the term Unicode. While ASCII is associated with eight bit character representation, Unicode can represent over a million characters, including emojis, using up to 32 bits. Unicode and its various forms such as UTF-8 and UTF-16 is beyond the scope of this course, and it isn't necessary for you to know about it at this stage.
Hallam is a software architect with over 20 years experience across a wide range of industries. He began his software career as a Delphi/Interbase disciple but changed his allegiance to Microsoft with its deep and broad ecosystem. While Hallam has designed and crafted custom software utilizing web, mobile and desktop technologies, good quality reliable data is the key to a successful solution. The challenge of quickly turning data into useful information for digestion by humans and machines has led Hallam to specialize in database design and process automation. Showing customers how leverage new technology to change and improve their business processes is one of the key drivers keeping Hallam coming back to the keyboard.