Course Intro & Overview
C# Data Types and Variables
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
Variables and data types have a kind of chicken and egg relationship. C# is a strongly typed language, meaning that variables must be of a specified data type. Let's start by looking at the line string myName = args. This is called a variable declaration, where you define a variable by declaring its existence and what type of value the variable will store or hold. A variable declaration starts with the date type, in this case, string, which means it will hold a text value, followed by the name of the variable.
Variable names can start with any letter or an underscore. Like can't start with a number. You don't have to initialize a variable with a value as part of the declaration, but it is considered best practice to do so. This means you can declare with just the data type followed by the name and a semicolon. You will need to give the variable a value before accessing it, so that the computer doesn't assign some arbitrary value.
The next question is why would some arbitrary value be assigned to a variable? Well, this leads us nicely into what a variable really is. A variable is a label identifying an address in memory and in the case of C#, a section of memory that can only contain data of the type the variable is declared to hold. I realized this may sound a bit abstract, so I'll show you what I mean by looking at the memory address of the myName variable. You definitely won't need to look at memory addresses for quite some time, if at all, but I think this will help you understand what I'm getting at.
I've opened up a memory window from within Visual Studio and got the program to stop executing after value has been assigned to the myName variable. When I type myName into the memory windows address bar, the variable name is replaced by an address location which is displayed as a hexadecimal, base 16 number. And the contents of the computer's memory at that location is displayed. Each one of those two-character pairs represent a byte of memory also in hexadecimal notation.
On the right is the data or information that those bytes of memory represent in human-readable form. There's the value Elon that we saw from the introductory course passed in as an argument that is stored in the myName variable location.
Without initializing the variable with a value, there is no guarantee that the variable location won't contain rubbish or nonsense when the memory address is allocated to the variable. Your program might incorrectly try to access the variables uninitialized rubbish data causing the program to crash.
Now is probably a good time to talk about bits, bytes, and hexadecimal. A bit is the most elementary representation of information in a computer. It is binary and can have the value one or zero. A byte is made up of eight bits and the significance of each bit is determined by its position within the byte. Starting from the right, each position of the byte doubles the value the bit can represent. To represent the number 69, we need one times 64 plus one times four plus one times one. The biggest number a byte can hold is 255, meaning it can represent 256 different values zero through to 255.
Earlier, I mentioned hexadecimal and that it was base 16. Decimal, base 10 uses the digits we are all familiar with representing 10 values, zero through to nine. Hexadecimal represents 16 values by using the first six letters of the alphabet to represent the numbers 10 through to 15. A equals 10, B equals 11, F equals 15. 69 in decimal is six tens plus a nine. 69 in hexadecimal is four sixteens, which is 64 plus a five giving us 45.
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.