C# Variables and Data Types – Part 1

This is the start of my C# tutorial series where we’ll dive into the basics of the language and work through understanding the fundamentals. I’ll be posting recommended books on the subject in each part. Feel free to check it out, if you decide to purchase please use my link as it supports my work here.

Covers the essentials and is a really good starting point. Nice reference if you’re like me and prefer to have a hard copy of something around to reference.

Part 1 Variables and Data Types

You can think of variables as small storage objects in your computer. The variables can be named such as first_name, last_name, age, location, phone number, etc.. modern computers have many of these objects and they reside in the memory or otherwise called RAM. Variables can hold many different types of data, they can also hold reference to another variable. Lets look at a simple definition of a variable.

int apples = 1;
int grapes = 5;
int fruit = apples + grapes;

In this examples first we define the variable type, which in this case is an integer. Next we define the name we want the variable to have. This name should be descriptive as to what type of data the variable is holding. So in this situation our integer is keeping track of the number of apples we have. We have a similar definition for grapes, however we have more grapes than apples as indicated by the value of 5. Finally if we wanted to track how much fruit we had in total, we could add the value of apples and grapes together in a new variable using the named definitions.

Data types come in all shapes and sizes, in the previous example we just worked with integers (int). Integers are defined as whole numbers (1,2,3, etc…). However, we can’t live in a world where things are just integers, when you go to the store and buy carton of milk, chances are it won’t cost a whole amount. This is where floating point numbers, doubles, and many other data types come in. These data types are designed to hold a specific range of values when programming. Its also important to note the single equals sign (=). A single equals sign in programming means assignment. So looking at apples integer from above, I’m assigning the value 1 to the variable apples. In future tutorials we’ll cover the double equals which is used for checking equivalence.

Here is a listing of all the data types from the Microsofts MSDN reference:

You can build fairly complex applications with just string, int and float. I’d recommend understanding all the datatypes and how they can be used, but stick the basics for now. Go practice on your own, see if you can build a console application that prints the sum of two integers or two floating point numbers. Post a screenshot here of your results!

Bonus Challenge: Capture input of the two numbers and print the output.

If you didn’t catch my post about setting up visual studio and creating a console application, check that post out here:C# – Visual Studio Setup Part 0



Leave a Reply