Archive for June, 2007

yield keyword

June 17, 2007

“Yield”=> Contextual keyword introduced to c# 2.0. It is important for “lazy evaluation” and the performance of queries in LINQ. Yield can be used as a variable name in C#. When put before return it becomes a keyword.

“Lazy evaluation”=>Lazy evaluation means that until you iterate over the result of the query, the source of the query is not iterated.

Advantages=>yield allows one enumerable class to be implemented in terms of another. This enables the delay of execution of queries until the latest possible moment, skipping the generation of intermediate results that would drastically reduce performance. The query operators in LINQ operate on sequence. The result of a query is often another sequence.

Var keyword in orcas

June 17, 2007

What is it??
As the name suggests, “var” keyword can be used in place of type name in local variable declarations.

Is it a untyped variable reference??
No, it is not a late bound or untyped variable reference. It always creates a strongly typed variable reference. When the var type variable is declared, the compiler infers the type of the variable from expression used to initialize the variable when it is first declared. for example, var name = “yoganand”; will be inferred as string name=”yoganand”;.The CLR actually never knows that the var keyword is being used – from its perspective there is absolutely no difference between the above two declarations but in the first declaration, the compiler does the work of inferring and declaring the type name.

What types can be referenced with var keyword??
Var keyword can be used to reference any type(anonymous and explicitly declared types) in C#.
Even custom types we define can be referenced.Var keyword produces strongly typed declarations and hence the compiler needs to be able to infer the type based on its usage. So, var definition should always be initialized, produces a compiler error otherwise.