By 22/07/2015 10 Comments

C# Variables

  • It is a free space in memory.
  • It is name of a memory space or storage area.
  • You cannot declare a variable which starts with any digit like (44c_sharp or c-sharp) etc.
  • It can be any valid name (e.g.: totalAmount, studentName) etc.

SYNTAX – 1:

data_type variable_name;

Example – 1, declaration:

string technologyName;

Description of above example:

STATEMENT DESCRIPTION
string It is a data type.
technologyName It is a name of a variable.
string technologyName; This statement called declaration, which declares a string variable technologyName, but at this point you cannot use this variable because you have only declared this variable and do not store any value to it.
technologyName It is a name of a variable.

SYNTAX – 2:

data_type variable_name = any_value;

Example – 2, initialization:

string technologyName = "Dot net ";

Description of above example:

STATEMENT DESCRIPTION<
string It is a data type.
technologyName It is name of a variable.
= This is an assignment operator (=) which assigns or store a value to a variable (assign from right to left).
“Dot net” This is a string value which is being assigned into technologyName variable.
string technologyName = ” Dot net “; This statement called initialization, which stores or assigns a string value “Dot net” into a variable (technologyName), now you can easily use this variable and also print it.

Syntax – 3:

You can also declare and initialize more than one variable of same data type in a single statement as following:

data_type variable1_name = any_value , variable2_name = any_value;

Example – 3:

string productName = "laptop" , userStatus = "off line";

Description of above example:

STATEMENT DESCRIPTION
string It is a data type.
productName It is name of a variable 1.
= This is an assignment operator (=) which assigns or stores a value to a variable (assign from right to left).
“laptop” This is a string value which is being assigned into productName variable.
, This is comma(,) which separates productName variable’s value from userStatus variable’s value.
string productName = “laptop” , userStatus = “off line”; This statement called initialization, which stores or assign a string value “laptop” into a variable (productName) and than another value “off line” is being assigned into variable (userStatus), there are a comma(,) between these 2 variables which separates both values. Notice, these both variables has a same data type which is a (string).

Syntax – 4:

If you want to declare or initialize the different types of variables, than you have to use separate statements as bellow:

data_type1 variable1_name = any_value;

data_type2 variable2_name = any_value;

data_type3 variable3_name = any_value;

Example – 4:

    string browserName = "google chrome";
    double pi = 3.14;
    bool check = false;

Description of above example:

STATEMENT DESCRIPTION
data_type1 Suppose this is a string data type which has a variable, named browserName and stores a value “google chrome”.
data_type2 Suppose this is a double data type which has a variable, named pi and stores a value 3.14.
data_type3 Suppose this is a boolean data type which has a variable, named check and stores a value false.

Example:

using System;

namespace csharpBasic
{
    class Program
    {
        static void Main(string[] args)
        {
            // 2 string type variables are intialized.
            string siteUrl = "https://www.tutorialstown.com", siteName = "tutorialstown";
            Console.WriteLine("Website name: {0}", siteName);
            Console.WriteLine("Website URL: {0}", siteUrl);

            // A double type variable is declared.
            double pi;

            // A double type variable is initialized.
            pi = 3.14;

            Console.WriteLine("Pi: {0}", pi);

            // double, booelean and an int type variables are initialized.
            double gravity = 9.8;
            bool isChecked = true;
            int seatNumber = 5288;

            Console.WriteLine("Gravity: {0}", gravity);
            Console.WriteLine("Is checked {0}", isChecked);
            Console.WriteLine("Seat number: {0}", seatNumber);

            Console.ReadKey();
        }
    }
}

Remember:

If you initialize a variable as following, you will get a syntax error and program will not be compiled.

// Compile-time error !
string operatingSystem = "windows 8.1" , int seatNumber = 02145;

  • This error is being generated because you are trying to initialize different types of values using multiple data types in a single line.
  • If you want to initialize different types of values using multiple data types, than follow example – 4 which has been described as above.






← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .