C# TUTORIAL

C# Variables in detail



Let us explore more about the variable types in C Sharp.The table below lists the predefined value types. Because in C# all of the apparently fundamental value types are in fact built up from the object type. The list also indicates which System types in the .Net framework correspond to these pre-defined types.

C# Type.Net Framework (System) typeSigned?Bytes OccupiedPossible Values
sbyteSystem.SbyteYes1-128 to 127
shortSystem.Int16Yes2-32768 to 32767
intSystem.Int32Yes4-2147483648 to 2147483647
longSystem.Int64Yes8-9223372036854775808 to 9223372036854775807
byteSystem.ByteNo10 to 255
ushortSystem.Uint16No20 to 65535
uintSystem.UInt32No40 to 4294967295
ulongSystem.Uint64No80 to 18446744073709551615
floatSystem.SingleYes4Approximately +/- 1.5 x 10-45 to +/- 3.4 x 1038 with 7 significant figures
doubleSystem.DoubleYes8Approximately +/- 5.0 x 10-324 to +/- 1.7 x 10308 with 15 or 16 significant figures
decimalSystem.DecimalYes12Approximately +/- 1.0 x 10-28 to +/- 7.9 x 1028 with 28 or 29 significant figures
charSystem.CharN/A2Any Unicode character (16 bit)
boolSystem.BooleanN/A1 / 2true or false


Let us take a look at the following example


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
	
	    int length;
            int breadth;
            int area;

            length = 20;
            breadth = 10;

            area = length * breadth;

            Console.WriteLine("Area of Rectangle is : {0}", area);
            Console.ReadLine();


        }
    }
}


If you run the above program you should be able to see an output on the dos prompt type console window which shows the area of the rectangle. Take a look at the definition of the length, breadth and area variables.


	
	    int length;
            int breadth;
            int area;

"Variables" are storage locations for data. You can place data into them and retrieve their contents as part of a C# expression. In the above example length, breadth and area are variables. The variables can be of any one of the several possible types. In the above example these variables are of type "int" wich stands for integer.

C# has a number of types that includes boolean, integral ( int, uint, long, char, uchar,sbyte, byte etc), Floating Point Types ( float, double, decimal), string type etc.

C# Operators


Take a look at the following line in the above example


        length = 20;
        breadth = 10;

	area = length * breadth;

In C# Results are computed by building expressions. These expressions are built by combining variables and operators together into statements. In the above example the first two statements assign values to variables length and breadth using "=" assignment operator. In the third line in the above example the variable area is assigned a value using = and * arithmetic operator.

This completes the basic tutorial of understanding the variables and operators. In the next chapter we will present a complete list of variables and operators for your reference.