C# Variables

In this chapter you will learn:

  1. What are variables
  2. Syntax to define C# variables
  3. Example for C# variables
  4. C# program for variable definition

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/*from w w  w.j a  v a2 s.  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.

Next chapter...

What you will learn in the next chapter:

  1. What are C# comments
  2. What are the three types of comments
  3. Single line comments
  4. Multiple line comments
  5. XML documentation comments
Home »
  C# Tutorial »
    C# Language »
      C# Hello World
C# Hello World Tutorial
C# Variables