Can we declare an array variable with var keyword in C# 3.0?

 Posted by Raja on 11/18/2009 | Category: C# Interview questions | Views: 7596

No, var keyword is used to declare implicitly typed local variable. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

So you can declare a local variable of any type using var keyword but you can't declare an array of variable using var keyword.

 string[] str = { "ram", "sham" };

foreach (var s in str)

Response.Write(s + "<br />");

var[] str = { "ram", "sham" };

foreach (var s in str)

Response.Write(s + "<br />");

This will throw "CS0246: The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?) " error.

For more info on var keyword see


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Raja on: 12/22/2009
Thank you very much Raja for sharing this info.
Posted by: Sachinpune8 on: 2/21/2015 | Points: 10
Good One.

Login to post response