C# Simplified
(Page 1 of 5 )
Want to learn C# but don't know where to begin? Look no further. This article, the first in a series of articles covering the language, will provide you with a good starting point.
Support file for this article is available here.
In this article, you will learn about:
- Data Types, Variables and Operators
- Value Types and Reference Types
- Classes
Data Types, Variables and Operators
Data Types and Variables are the core of the C# programming language. They represent how to express numbers, characters, strings and other values in real code. For example, if you want to add two numbers, such as 100 and 200, you must write a code as shown below:
Listing 1.1
int X = 100;
int Y = 200;
int Z = x+y;
Console.WriteLine(Z);
The above code is not a complete listing but it shows the way you have to declare values.
In listing 1.1, the term int represents the built-in data type called Integer. The int is an alias for the Integer data type. Like Integer, there are lots of other data types which you can use for your programming tasks.
You will also notice three characters named X, Y and Z in the above code. They are known as variables. X, Y and Z are the names given to the variables. Variables are used to store data in the memory. Without variables you cannot store data and perform manipulations. They are the containers for the real data. 100 and 200 are the values given for the respective variables. The end result is stored in another variable called Z.
Each data type has a fixed range beyond which you cannot store values. If you attempt to store a big value for an Integer data type, the program will emit errors. Consider a scenario where you are storing a value of 500 using a Byte data type. The program will not show any errors during the compilation stage since the basic logic is correct. Instead, there will be runtime errors because you are storing too high a value for the byte data type. It will only hold values up to 255. These errors are called as Exceptions. You will learn more about exceptions in a later article.
You must always use the data type best suited to your programming needs. For example, if you are writing code for developing a calculator, it’s better to use data types which will accept large values. This will help you to avoid costly runtime errors.
Next: Different Types of Data Types >>
More C# Articles
More By Anand Narayanaswamy