Interview Questions PART1-IEnumerable,VAR,DYNAMIC,READONLY,CONST Keywords

IEnumerable and IQueryable

1) IEnumerable is suitable for performing iteration on in memory collection like list,array
2)While querying data from the database, IEnumerable executes "select query" on the server-side, loads data in-memory on the client-side and then filters the data.
3)If you create an IEnumerable, then all rows will be pulled into memory as objects before running the query.


1)IQueryable is suitable for querying data from out-memory (like remote database, service) collections.
2)While querying data from a database, IQueryable executes a "select query" on server-side with all filters.
3)If you create an IQueryable, then the query may be converted to sql and run on the database server 


VAR and DYNAMIC KEY WORD in .NET

//VAR has to be initialized at compiletime
            //VAR cannot be reinitialized more that once
            var str = "hello";
            Console.WriteLine(str);


            //dynamic is not required to be initialized at compiletime
            //dynamic  can be reinitialized more that once because the type is declared for hindi
            dynamic str2;
            str2 = "Hello";
            Console.WriteLine(str2);

            str2 = 2;
            Console.WriteLine(str2);


READLY and CONSTANT WORD in .NET

Constants are immutable values which are known at compile time and do not change their values for the life of the program.
Constants can be accessed using classname,variablename
Constanct variables need to be initialised during compile time
the Value assigned to it must be known at compile time

Readonly variables are also immutable values which are known at run time and do not change their values for the life of the program.
Readonly can be accessed using instance name.
Readonly variables can be initialized during runtime through constructor only
the Value assigned to it must be known at run time

class Dynamic_and_Var
    {
        public const string str = "hello I am From const"; //both are immutable that means they can be changed while running the program
        public readonly string rd = "hello I am From readonly";


        //Readonly variable are assiged value only through constructor           
        public Dynamic_and_Var(string stri)
        {
            rd = stri;
           
        }

        public Dynamic_and_Var()
        {
        }
    }

/////////////////////////////////////////////////////////////////////////////////////////////////////////
Inside Main
-----------------

 Console.WriteLine(Dynamic_and_Var.str);

            Dynamic_and_Var obj = new Dynamic_and_Var();
            Dynamic_and_Var obj1 = new Dynamic_and_Var("I can change and I am readonly");

            Console.WriteLine(obj.rd);//= "hello I am From readonly";
            Console.WriteLine(obj1.rd);
           
////////////////////////////////////////////////////////////////////////////////////////////////////////

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6