C# Variables

Description

Variables are used to store values.

Syntax

You declare variables in C# using the following syntax:

datatype identifier;

Example

For example:

int i;

This statement declares an int named i.

After it has been declared, you can assign a value to the variable using the assignment operator, =:

i = 10;

You can also declare the variable and initialize its value at the same time:

int i = 10;

If you declare and initialize more than one variable in a single statement, all of the variables will be of the same data type:


int x = 10, y =20;   // x and y are both ints 

Example 2

Let's create another C# program.


class Program//w w w.  j a v a2s.  c  o  m
{
    static void Main(string[] args)
    {
        int i = 0;
        int j = 2;
        
        System.Console.WriteLine("i=" + i);
        System.Console.WriteLine("j=" + j);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    C# Language »




C# Hello World
C# Operators
C# Statements
C# Exception