Course Intro & Overview
C# Data Types and Variables
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
Let's head back to the hello mars program and put into practice what we've learned. I'll remove the initialization of the myName variable with args. As soon as I've done that, we get a squiggly red line under the variables first use, indicating that we are attempting to use an uninitialized variable. I'll give myName the value of "Hello" to fix that.
Next, I'll declare another string variable called newLine, and assign the .NET Environment.NewLine property, which is also a string to that variable. In the past, you would have just used "\r\n", but as .NET is cross-platform and different operating systems use different characters to signify a new line, using this property will make your code more portable, that is, being able to run the same code on different platforms.
I'll change the WriteLine method, which makes sure following text output goes on a new line of Console to Write and then explicitly add the newline variable to the end of myName. Yes, this is contrived, and by doing it this way, I am essentially replicating the functionality of WriteLine with Write.
Using the plus symbol is one way of joining strings together. The correct term for joining strings is concatenation, and while adding strings together this way is easy, it is not the only or best way to join them. More on that later.
I'll run that to make sure it is working as expected. You can access each character of a string by using a square bracket after the string variable with a number. The first character of a string is always at position zero, not one. Now I'll write out the word "Hello" with the new line one character at a time.
After each line, I'm writing a comment. A comment is there to help others understand your code and for you to remind yourself of what you intended your code to do. A comment on a single line starts with two forward slashes. I'm just saying what each line is expected to write. Normally you would only comment code where it's not immediately obvious what is going on. When I run the program now, we have two "Hello"s, one corresponding to the myName variable and one to each letter being written out. You can change characters within a string, but not directly, as in assigning a character to a position in a string. This is because strings are immutable, that is they can not be changed.
When I added the newline string to the end of the myName variable, I didn't change myName. What happened was a new instance or version of myName was implicitly created corresponding to the myName on the left of the equal sign that is a concatenation of the original myName and newline variables. The myName variable name or label cease to point at the memory location of "Hello", and now points at where "Hello" plus newline is located in memory. This is a subtle but important distinction that we shall come back to in the future.
The other issue is what happens to the data or memory of the original myName variable on the right side of the equal sign. .NET has built in garbage collection, which means you don't have to worry about it, as .NET will free up all discarded memory that is no longer being used automatically.
Automatic freeing of discarded memory is a very big deal. It's taken for granted in many programming languages now but back in the day, and still with C and C++, memory leak errors are some of the hardest bugs to track down. A memory leak is where the failure to free no longer use memory back to the operating system, causes a program to consume more and more memory, eventually bringing the computer to a halt.
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.