Variable

A variable stores value and variable type defines the behaviours of the variable.

The following code uses the int type variable to calculate the result of 2 plus 2.


using System;

class Program
{
    static void Main(string[] args)
    {
        int i = 2;
        int j = 2;

        int result = i + j;
        Console.WriteLine("result=" + result);
    }
}

The result:


result=4

In the code above, i and j are two variables. The + (adding) is the behaviour.

int is a predefined data type in C#.

Another predefined data type is string. You can store a list of characters in the string type variable.


using System;

class Program
{
    static void Main(string[] args)
    {
        string str = "demo from java2s.com";

        Console.WriteLine("str=" + str);
    }
}

The result:


str=demo from java2s.com

The string type variable can be converted to its upper case form:


using System;

class Program
{
    static void Main(string[] args)
    {
        string str = "demo from java2s.com";

        Console.WriteLine("str=" + str.ToUpper());
    }
}

The output:


str=DEMO FROM JAVA2S.COM
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.