Thursday, January 3, 2013

What is the use of var keyword in c#?


var keyword

In C# method scope, a variable can be declared as two types.

  1. Implicit
  2. Explicit

Explicit means you know the data type like integer, string etc and Implicit means you don’t know the data type.

Example

// Explicit data type
Int i = 5;
string str= “Anjali”;

// Implicit data type
var i=5;
var str=”anjali”;

Note: Var is a statically data type means the data type determine at compile time, not at runtime. Var is a strongly typed means data type can not be changed once it has been declared.

Var i =100; // Correct
I = “Welcome”; // Incorrect

Definition: Var keyword is an implicit way to define the data type. Compiler determines data type when application gets compiled.

No comments:

Post a Comment