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
In this course, we have learned what a variable is and that C# as a strongly typed language, meaning that a variable must be declared as a particular data type and can only store values of that type. We have seen the most elementary data types that C# supports, as in text, numbers, and boolean values.
Text data types are strings and chars, where a string is an array of chars. C# or more precisely, .NET, stores text characters as 16 bit unicode, allowing built in support for multiple character sets. A string can be made up of smaller character sequences concatenated together using the plus symbol. You can also embed variables within a string using interpolation, enclosing the variables in curly braces.
.NET and C# support integer and floating point numbers. And within each of these number categories there are multiple data types that differ in both size and numerical precision. There are four integer data type sizes, one, two, four and eight bytes, with each having a signed and unsigned variant, giving us a total of eight integer data types.
In terms of floating point number data types, we have float at four bytes, double at eight bytes, and decimal at 16 bytes. Representing fractional numbers in the computer's binary environment has always been problematic.
We investigated some of the rounding issues that may arise depending on the data type you choose to represent a particular number. Decimal, while not being able to represent the greatest range of floating point numbers, it does represent them with the highest precision.
We also saw how to get the compiler to view one data type as another by casting it from one type to another. The bool type is used to represent a binary state, usually true or false. We did briefly touch on boolean operators for comparing values, and we shall explore that in far more depth than future courses.
We finished off the course by looking at arrays, which is a way to group values of a particular data type within one variable, where you can access each value with an index. When looking at arrays, we discovered that a string is essentially an array of characters which is also valid outside of a computer. As I said, an array is more than just a way to reference multiple values of particular data type. We saw that it has properties such as length.
In C#, arrays and strings are far more than just data types or collections of variables. They are classes. In addition properties, classes have methods that perform actions, and we saw that with the two string method of .NET's convert class, in the integer demo. C#is an object oriented language, and at the heart of object orientation are classes.
In the next course, we look at the fundamentals of classes and object orientation. See you there.
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.