Using Variables and Data Types in C# Programming

C# (3)- Variables and Data Types

In the past two articles, we had a brief introduction to the C# language: why and how you could use it, and what sorts of applications can be built with it. You had also got your hands dirty, and created your first C# program.

In this article, we are going to discuss one of the main building blocks of the C# language (and any programming language actually): Variables.

You can think of a variable just like a container, or a bucket that can and may contain data. Those buckets are placed in the computer’s memory, where they can be stored and retrieved by the processor.

As you may know, inside the computer’s memory, there is nothing but zeroes and ones (Data, any data is represented inside the computer circuitry as a series of the 0’s and 1’s). This is how a variable or a number of variables are actually stored. So a series of bits like 00101110 is in fact a variable of some type.

Types of variables
Since buckets have different sizes and, hence different usages, variables (the friendly names of such buckets) also have different types and usages. This is useful in data manipulation; for example it won’t make any sense if you treat a series (bytes) of data as an image file, where in fact they represent a Microsoft Word File. Would it?!!

In C#, you cannot use variables unless you declare them. Declaring a variable means informing the compiler (a program responsible for converting your code into a machine code that is understandable by the machine) that you intend to use the variable named xyz as an integer for example. Some other languages do let you do this. Those languages are, hence, called loosely typed languages. But C# is not one of them. To declare a variable in C#, the following syntax is used:

int myVariable;

Where int is the type of the data to be stored in this variable (we’ll get to that in a moment), and myVariable is the name of the variable. Now, you can assign appropriate values to your variable like:

myVariable = 210;

In C#, we have two main categories of data types for variables: Simple data types and Complex data types.

Learn Cloud Computing from Scratch for Beginners

Simple Data types
They include numbers and Booleans (true or false). Numbers in turn are divided into several data types, and this might sound counterintuitive at first because of the natural question: why have different types of variables of all what they are going to hold is just numbers? The reason for this is the way numbers are stored in memory. Let’s consider our previous example. How is the number 210 stored in a computer memory? As mentioned before, the computer can understand only 1’s and 0’s. Accordingly, 210 should be converted to the form that can be understood by the computer hardware. The following is how the same data is seen by the human and the computer system:

Human Computer
210 11010010

This is called the binary numbering system. We may explain it in detail in a later article, but suffice now to know that this is how the computer stores the number 210 in memory. Every digit of 11010010 is called a bit. So the number 210 fills 8 bits of memory.

But what about the number 1.256546? or the number 987225698? They will be using more than 8 bits of memory. So how much memory should you allocate for number? That’s why we have the following integer (whole numbers) variable types in C#:

Alias Type Range
Sbyte System.SByte -127 to 127
Byte System.Byte 0 to 255
Short System.Int16 -32768 to 32767
ushort System.UInt16 0 and 65535
Int System.Int32 −2147483648 and 2147483647
Uint System.UInt32 0 and 4294967295
Long System.Int64 −9223372036854775808 and 9223372036854775807
ulong System.UInt64 0 and 18446744073709551615

As you can see, you have variables for all sorts of integers: from the tiny ones to the most huge ones. But what about non-whole numbers (decimals)? They are handled differently in C#, and have their own set of variable types:

Alias Type Range (approximately)
float System.Single 1.5 x 10-45 to 3.4 x 1038
double System.Double 5.0 x 10-324 to 1.7 x 10308
decimal System.Decimal 1.0 x 10-28 to 7.9 x 1028

Choosing the right type of variable is what makes a good programmer. For example, assigning a variable that you know it will never hold more than 100, and will always be positive, to a long type variable is definitely a waste of memory.

To complete our discussion of simple data types, we have the char, the bool, and the string. They are depicted in the following table:

Alias Type Description
char System.Char Single unicode character
bool System.Boolean Holds true or false
string System.String A sequence of characters

The char is just a character. A character is different from a letter as the character can hold letters in addition to punctuation marks (#$%^…etc.). C# regards the char variable type as unicode. This means that it is not bound to English language letters and symbols but also all other languages that are covered in the Unicode character table. For more information about unicode, please refer to this link

The bool type is one of the most commonly used variable types not only in C# but in any programming language. Imagine how many conditions your application has to manage when it’s working; like “Do you want to save?” or may be “Are you sure you want to exit?”.

The string type is also very widely used. It simply holds a sequence of characters. So “My name is Joseph” is a string.

Now I guess you’re got bored from the long discussion, let’s write some code.

In Visual Studio, create a new console application and add the following code to Program.cs:

static void Main(string[] args) {
	int myInteger;
	string myString;
	myInteger = 71;
	myString = "myInteger is";
	Console.WriteLine("{0} {1}.", myString, myInteger);

Now press start debug button (the button with the green play symbol) to launch the application. You find the following output on the screen:

myInteger is 71

Ok you did a lot this time than simply displaying a “Hello World” message to the user! The first two lines of code declared the variables you’re intending to use: an integer, and a string. Then you assigned values to those variables using the equal sign (=). Now you variables contain values, let’s do something with them: You used the WriteLine method of the Console class to print the output of the two variables to the screen. Notice the use of placeholders enclosed in curly braces {}. This is just a way to inject variables in the output of WriteLine. In other words, I could have wrote it this way:

Console.WriteLine(myString + " " + myInteger)

It would have given the same output. Here I am using the + sign to concatenate variables (you’ll know more about this when discussing strings). But as you can see, the first method is much neater and is less error-prone.
Can I give any name to my variables?
Yes, you can name your variables however you like, but you have to watch for the following conditions:

  • You can start the name with only a letter, an underscore (_), or an at sign (@).
  • The rest of the name can contain only letters, numbers, or an underscore.
  • C# has some words with special meanings (reserved keywords) that cannot be used as a variable name. Example: namespace, using, and System. If you try to use one of those as variable names, the compiler will raise an error and the code will not work.

So myNewVar, student1, and _data are all valid variable names, while 123var, using, and user’sname are not.
In this article we discussed how C# deals with variables, the simple types of variables, and how the language classifies numeric variable types. Then we had a look at non-numeric simple variable types, and their typical usage. You wrote a C# application where you assigned and used string and int variable types.

You are encouraged to write more code like the example, and try the other variable types to firmly understand the concept.

I hope you enjoyed this article.


Please enter your comment!
Please enter your name here