Variable and data types

  • Post author:
  • Post category:csharp-category
  • Reading time:9 mins read

Variable and data types


A variable is simply a name given to a specific location in the computer which stores the data.

Data types

A data type of computer science or programming is simply a classification of data. It tells the compiler or interpreter how the programmer wants to use the data. Most programming languages use some common data types to hold the data. Some common data types used in C# are given in table 1.1.

      Table 1.1

Data Type Description Size in Memory (bytes) Range
int Used to store numeric values 4 -2 billion to 2 billion
double Used to store 64-bit floating-point values  8 15 significant digits2
float Used to store 32-bit floating-point values 4 7 significant digits1
char Used to store a single character 2 Unicode characters
string Used to store text or more than one character 4 Length up to 2 billion bytes
decimal Used to store the  128-bit floating-point values 24 28 to 29 significant digits
bool Used to store the Boolean type values (e.g. True or False) 1 true, false
long Used to store larger numeric values 8 -9 quintillion to 9 quintillion
byte Used to store small numeric values 1 0 to 255
short Used to store small numeric values ( -32,768 to 32,767) 2 -32,768 to 32,767

C# has some other data types but they are not commonly used data types.


Variables are simply a name given to a specific location in the computer which stores the data. Variables must have a data type that it intends to store. Let’s take a look at the simple variable in C#.

   int age;

Here age is variable of type Integer. An integer type variables occupy the 4 bytes of memory. When declaring variables in C# it follows the following syntax.

<data type> <variable name>;

You can declare a variable first then assign it a value at any time later in the program.

<variable name>=<value>;

Variable can be declared and initialized at the same time. E.g.

<datatype> <variable name> = <value>;

Below is given a fully working example that how to use variables in C#.

   using System;
   namespace Zitoc
     class UsingVariables
        public static void main(String [] args){
        string name="Ali";

In this program, we have declared a variable name of type string. This variable stores a text which has the name of any person. Then we printed out the value of variables on the console screen by using the Console.WriteLine() .

You can assign a value of one variable to other variables if they are of the same type. E.g.

   class UsingVariables
        public static void main(String [] args){
        string name="Ali";
        string name2="Hassan";

In the above example, we declared another name2 variable of type string and assigned it a value. In the next line, we assigned the name2 variable to the name variable. Now it will print out Hassan instead of Ali on the console screen.

You can declare multiple variables on the same line separated by commas.

   int i,j,k,l;

A value must be assigned to a variable before using it otherwise it will be a compile-time error. For example below we have declared a variable called num without assigning any value to it. When we try to print the value of this variable it will give a compilation error, because we didn’t store any value in it.

   int num;
   //Following will give compile time error: "Use of unassigned local variable 'num'"

Points to remember

  • Variables name must start with an alphabet or underscore
  • Always initialize variables before using it
  • Multiple variables can be declared at once separated by a comma
  • The value of variables can be changed at any time in the program
  • Always store the same type of value as the type of variables


We are a team of writers, researchers, and editors who are passionate about helping others live their best lives. We believe that life is a beautiful gift. We try to live our lives to the fullest and enjoy every moment. We are always learning and growing, and we cherish the relationships we have with our family and friends.

Leave a Reply