3

What is the difference between var and string in C#?

tariq
  • 2,193
  • 15
  • 26
John
  • 697
  • 4
  • 13
  • 27
  • Possible duplicate of [What's the point of the var keyword?](http://stackoverflow.com/questions/209199/whats-the-point-of-the-var-keyword) or even [Use of var keyword in C#](http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp) (downvote because you question could be answered easily with the help of google or msdn) – Tim Schmelter May 28 '13 at 10:54
  • I don't why some guys Vote down this Question. I know its silly question but working with doubts will never improve your coding. So that's why I was asking. – John May 28 '13 at 11:23

6 Answers6

15

The keyword var is used for declaration of implicit types. If you are using a named type for the variable, there is no other difference than readability (and opinions differ on which is better). Example:

var s = "asdf";

gives exactly the same result as:

string s = "asdf";

However, if you have an unnamed type, you have to use var to declare a variable that has that type. Example:

var o = new { s = "asdf" };

There is no corresponding declaration using a named type, because the type of the object that is created doesn't have a name.

The var keyword is often used with LINQ and LINQ extension methods, when the result doesn't have a named type. Example:

var result = someList.Where(x => x.Age < 10).Select(x => new { name = x.Name });
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
5

var is an implicit type and string is an explicit type but these declarations are functionaly equivalent

var implicitString = "hello";
string explicitString = "hello";

Only variables that are declared at method scope can have an implicit type var. When used, compiler infers the type from the right side of the assignment.

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • Actually some time we store data source in var (like below scenario). So I am confused that it stores sequence of character or work as dataTable or ArrayList. var qurey= datasource();//order by page id if(qurey != null) { ddlpagenubers.DataSource = qurey; – John May 28 '13 at 11:00
  • @John: it stores whatever `datasource()` returns. Check method return type to be sure. – Claudio Redi May 28 '13 at 11:06
2

First off, these are C# keywords, not ASP.NET.

string is a type - an explicit type that basically gets replaced by the compiler with System.String.

var is a keyword that stands in for a type - when the compiler can tell what the type of a variable should be (say from the return type of a function, or a literal). If you use a string, the compiler will make the same replacement as above.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

Var is used to declare Implicitly typed variable where the compiler identifies the type automatically. var can hold any type of data. String is an explicit declaration for holding string data only.

var i=5; 
var a="abcd"; 
var b= 5.5555; 
var c=new class(); 


String s = "ABCD";
rags
  • 2,580
  • 19
  • 37
2

There is no legitimate comparison between var and string.

'var' is used for declaring implicit types be it an int , string , double , an object etc etc. It is useful when you don't exactly know the return type of a method Or you are not concerned with it at all.

'string' is a particular data type which can hold only string data and not of any type.

Also for a var the type must be defined at the time of declaration of var variable i.e at compile time only, otherwise the compiler will flag compile time error. Hence while

string abc;

is possible but

var abc;

is not possible.

tariq
  • 2,193
  • 15
  • 26
0

There is no actual difference in VB.NET, there is another discussion with more info

Community
  • 1
  • 1
Ted
  • 3,985
  • 1
  • 20
  • 33