By 22/07/2015 12 Comments

C# Implicitly Typed Variables

  • These types of variables do not have normal data types such as int, double, string etc.
  • These variables are called implicitly typed variables.
  • These variables can be declared using the var keyword and must be initialized.
  • The c# compiler uses the initialization part to determine the type of a variable.
  • These types of variables are automatically changing their types according to given value.

Example:

using System;
namespace csharpBasic
{
    // start class definition / declaration.
    class Program
    { 
       // static main method void type declaration.
        static void Main(string[] args)
        {
            // Declare and initialize an implicitly typed variables.
            var number = 100;
            var movieName = "spider man";

            // Print the result 100.
            Console.WriteLine(number);

            // Print the result "spider man".
            Console.WriteLine(movieName);    
            Console.ReadKey();

        }   // End of main method definition.
    }   // End of class.
}

Remember:

  • Implicitly typed variables are used in some special situations.
  • These variables are mostly used for LINQ Language-Integrated Query (LINQ) (a c# feature).
  • We should use explicitly type variables because these are easy to understand and reading.
  • We can declare only one implicitly typed variable at one time.

Example:

using System;
namespace csharpBasic
{
    // start class definition / declaration.
    class Program
    { 
       // static main method void type declaration.
        static void Main(string[] args)
        {
            // Initialization of multiple implicitly typed variables.
            var pi = 3.14, gravity = 9.8; // compile-time error!
            Console.ReadKey();

        }   // End of main method definition.
    }   // End of class.
}





Next Tutorial →


← 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 .