Basic Data Types in C#
We will explore the data types available in C# and apply those data types by exploring variables.
Kinds of Data in C#
There are two kinds of data in C#:
-
Value Types which includes implicit data types, structs, and enumeration. These are passed to methods by passing an exact copy.
-
Reference Types which includes objects and delegates. These are passed to methods by passing only their reference (handle).
Variables
When a program is being executed, the data being used in that program is temporarily stored in memory.
A variable is the name given to a memory location holding a particular type of data.
Each variable has associated with it a data type, name and value.
In C#, variables are declared as:
<data type> <variable name>;
example:
int x;
On our example, we are reserving a memory to store an integer type value, which will be referred to in the rest of program by the identifier ‘x’.
You can initialize the variable as you declare it (on the fly) and can also declare/initialize multiple variables of the same type in a single statement as the examples below:
bool isReady = true;
double percentage = 90.11, average = 43.5;
char digit = '7';
In C#, as in many other programming languages, variables must be declared first before using them.
There is also a concept in C# where a variable should be initialized before its allowed to be used. Consider our failing code below:
static void Main(string[] args)
{
int age;
Console.WriteLine(age);
}
Variables and Methods Naming Convention
There is a suggested convention in naming variables and methods.
In C#, the convention use for variables is Camel Notation (first letter in lowercase) and for methods the Pascal Notation is used (first letter in uppercase).
After the first word, each word of both variables and methods should start with a capital letter, as in the example below:
//variables
age
fullName
//methods
PrintGrade()
Compute()
🔚 end of document 🔚