Subscribe

Variables in C#

Posted by Administrator on Saturday, July 2, 2011

Using variables is one of the key concept when programming.

What are variables?
          Variables are used to store values,data within our programme. We need to store numbers, characters, strings, dates and many more things withing our programme. To store them, we have to declare variables.
Variable has a datatype,name and the value.

  • Datatype - The type of the variable ( number, character, string, date or etc..)
  • Name - The identifier of the variable (Note that variable naming follows Camel casing)
  • Value - The value assigned to the variable

Syntax to declare variables: 
<datatype> <variableName>;

Assign values to variables: (initialization)
<variableName> = <value>;

 We can  declare and the initialize the variable at the same time

<datatype> <variableName> = <value>;

//declare and initialize when need
string name;
name = "my name";

//declaration + initialization 
string name = "my name";

C# supports many inbuilt data types or we can create custom data types as well.
Here are the C# in-built data types.

Data Type
Range
byte0 .. 255
sbyte-128 .. 127
short-32,768 .. 32,767
ushort0 .. 65,535
int-2,147,483,648 .. 2,147,483,647
uint0 .. 4,294,967,295
long-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
ulong0 .. 18,446,744,073,709,551,615
float-3.402823e38 .. 3.402823e38
double-1.79769313486232e308 .. 1.79769313486232e308
decimal-79228162514264337593543950335 .. 79228162514264337593543950335
charA Unicode character.
stringA string of Unicode characters.
boolTrue or False.
objectAn object.


Exercise: Write a programme that accepts your name, age and print them on the console.

Code:
using System;


class Program
{
    static void Main(string[] args)
    {
        //Declare a variable to store name 
        string name;
        //Declare a variable to store age
        int age;

        //Getting name from the user
        Console.Write("Enter your name? ");
        name = Console.ReadLine();

        //Getting age from the user
        Console.Write("Enter your age? ");
        age = Convert.ToInt32(Console.ReadLine());

        //Print name and age
        Console.WriteLine("My name is " + name);
        Console.WriteLine("I am " + age + " years old.");

    }
}

Save the file as Program.cs any where you want. You can compile c# using the command line version.

csc Program.cs

And then run the new program by entering

Program

Output:




Declaring constants

Constant has a value that does not change. They are declared as a field. C# uses the const keyword before the type of the field.And the most important thing is constants must be initialized as they are declared.

/* declaring a constant
* should initialize as declared
* value does not change*/
const double PI = 3.14159;

/* Error you cannot change the 
* value of a constant*/
PI = 3.14;


An Example:

using System;


class Program
{
    static void Main(string[] args)
    {
        const double PI = 3.14159;
        double radius = 10;
        double areaOfTheCircle = PI * radius * radius;

        Console.WriteLine("Area of the circle is " + areaOfTheCircle);
    }
}

Output:

0 comments:

Subscribe to: Post Comments (Atom)