Implicitly Typed Variables in C#.NET

 Implicitly Typed Variables

Implicitly typed variables are those variables which are declared without specifying the .NET type explicitly. In implicitly typed variable, the type of the variable is automatically deduced at compile time by the compiler from the value used to initialize the variable. The implicitly typed variable concept is introduced in C# 3.0.

The implicitly typed variable is not designed to replace the normal variable declaration, it is designed to handle some special-case situation like LINQ(Language-Integrated Query) 

we are declaring implicitly type variables using "var"

·        implicitly variabes are local variables because we cant declare them inside of class or cant pass to method

·        parameter or cant use it for return type of a method. 

Example

var i=10;

·        It is not allowed to use var as a field type in class level.

·        In C#, one cannot declare implicitly typed variable without any initialization like

var a; 

·        It is not allowed to use a null value in implicitly typed variable like

var a =null; 

 

Demonstration

/*

       Title        : Implicitly Typed Variables Demo

       CreatedBy    : Ramana Reddy

       CreatedDate  : 2023-July-07  

*/ 

using System; 

namespace ImplicitlyTDemo

{

    class ITDemo

    {

        static void Main(string[] args)

        {

            // implicitly typed variables

            var i = 10;

            var a = "Welcome to implicitly Typed Demo";

            var d = 123131.33d;

            var c = 'A';

            var f = 12313.4f; 

 

            // printing above declared variables

            Console.WriteLine("\n________________________________________");

            Console.WriteLine("\n " +

                "var i = 10;\n " +

                "var a = \"Welcome to implicitly Typed Variable Demo\";\n " +

                "var d = 123131.33d;\n " +

                "var c = 'A';\n " +

                "var f = 12313.4f;");

 

            // Getting of relevant data type 

            Console.WriteLine("\n_________________________________________");

            Console.WriteLine("\nThe relevant data types assigning at run time are \n");

            Console.WriteLine("i variable data type: " + i.GetType());

            Console.WriteLine("a variable data type: " + a.GetType());

            Console.WriteLine("d variable data type: " + d.GetType());

            Console.WriteLine("c variable data type: " + c.GetType());

            Console.WriteLine("f variable data type:" + f.GetType()); 

            Console.ReadLine();

        }

    } 

}

 

Nullable Types

As you know, a value type cannot be assigned a null value. For example, int i = null will give you a compile time error.

C# 2.0 introduced nullable types that allow you to assign null to value type variables. You can declare nullable types using Nullable<T> where T is a type 

Example:

Nullable<int> i = null;

A nullable type can represent the correct range of values for its underlying value type, plus an additional null value. For example, Nullable<int> can be assigned any value from -2147483648 to 2147483647, or a null value. 

The Nullable types are instances of System.Nullable<T> struct. Think it as something like the following structure.

public struct Nullable<T> where T : struct

{       

    public bool HasValue { get; }    

    public T Value { get; }       

    // other implementation

} 

Example:

Nullable<int> i = null;

    if (i.HasValue)

        Console.WriteLine(i.Value); // or Console.WriteLine(i)

    else

        Console.WriteLine("Null");

Comments

Popular posts from this blog

Email Sending through O365 using OAuth Protocol

IoT Technology

What is reverse proxy?