What does “var” mean in C#? [duplicate]2019 Community Moderator ElectionUse of var keyword in C#How does 'var' get resolved in C#IQueryable in C# and its usageXDocument compared to varC#. Instantiating an array: IComparable[]Parsing JSON list to int array in c#Pass a var type variable to a function as reference?Linq to SQL brief questionHow to return an int array of a given length that comes from PIWriting the first 3 letters of a List<> in C#What is the difference between String and string in C#?What does the [Flags] Enum Attribute mean in C#?Cast int to enum in C#How do you give a C# Auto-Property a default value?How do I enumerate an enum in C#?How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?What are the correct version numbers for C#?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How to loop through all enum values in C#?How do I generate a random int number?
Matrix using tikz package
My friend is being a hypocrite
How are passwords stolen from companies if they only store hashes?
Recruiter wants very extensive technical details about all of my previous work
Can a wizard cast a spell during their first turn of combat if they initiated combat by releasing a readied spell?
How to terminate ping <dest> &
Is there a term for accumulated dirt on the outside of your hands and feet?
What exactly term 'companion plants' means?
In what cases must I use 了 and in what cases not?
Maths symbols and unicode-math input inside siunitx commands
How do hiring committees for research positions view getting "scooped"?
What can I do if I am asked to learn different programming languages very frequently?
What does "Four-F." mean?
Could Sinn Fein swing any Brexit vote in Parliament?
Does the attack bonus from a Masterwork weapon stack with the attack bonus from Masterwork ammunition?
Unfrosted light bulb
Tikz: place node leftmost of two nodes of different widths
Generic TVP tradeoffs?
World War I as a war of liberals against authoritarians?
Am I eligible for the Eurail Youth pass? I am 27.5 years old
Bash - pair each line of file
Should I use acronyms in dialogues before telling the readers what it stands for in fiction?
Is it possible to stack the damage done by the Absorb Elements spell?
Calculate the frequency of characters in a string
What does “var” mean in C#? [duplicate]
2019 Community Moderator ElectionUse of var keyword in C#How does 'var' get resolved in C#IQueryable in C# and its usageXDocument compared to varC#. Instantiating an array: IComparable[]Parsing JSON list to int array in c#Pass a var type variable to a function as reference?Linq to SQL brief questionHow to return an int array of a given length that comes from PIWriting the first 3 letters of a List<> in C#What is the difference between String and string in C#?What does the [Flags] Enum Attribute mean in C#?Cast int to enum in C#How do you give a C# Auto-Property a default value?How do I enumerate an enum in C#?How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?What are the correct version numbers for C#?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How to loop through all enum values in C#?How do I generate a random int number?
Possible Duplicate:
Use of var keyword in C#
In C#, how does keyword "var" work?
c#
marked as duplicate by Matthew Jones, Rex M, Fredrik Mörk, Henk Holterman, John Hartsock Dec 1 '10 at 1:45
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
Possible Duplicate:
Use of var keyword in C#
In C#, how does keyword "var" work?
c#
marked as duplicate by Matthew Jones, Rex M, Fredrik Mörk, Henk Holterman, John Hartsock Dec 1 '10 at 1:45
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
Possible Duplicate:
Use of var keyword in C#
In C#, how does keyword "var" work?
c#
Possible Duplicate:
Use of var keyword in C#
In C#, how does keyword "var" work?
c#
c#
edited Aug 9 '17 at 14:37
Bhargav Rao♦
30.7k2092114
30.7k2092114
asked Nov 29 '10 at 19:54
alansiqueira27alansiqueira27
3,33684287
3,33684287
marked as duplicate by Matthew Jones, Rex M, Fredrik Mörk, Henk Holterman, John Hartsock Dec 1 '10 at 1:45
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Matthew Jones, Rex M, Fredrik Mörk, Henk Holterman, John Hartsock Dec 1 '10 at 1:45
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
It means that the type of the local being declared will be inferred by the compiler:
// This statement:
var foo = "bar";
// Is equivalent to this statement:
string foo = "bar";
Notably, var
does not define a variable to be of a dynamic type. So this is NOT legal:
var foo = "bar";
foo = 1; // Compiler error, the foo variable holds strings, not ints
var
has only two uses:
- It requires less typing to declare variables, especially when declaring a variable as a nested generic type.
- It must be used when storing a reference to an object of an anonymous type, because the type name cannot be known in advance:
var foo = new Bar = "bar" ;
You cannot use var
as the type of anything but locals. So you can't use the keyword var
to declare field/property/parameter/return types.
3
This is a good example of what answers need to be like on Stack Exchange. Sometimes people omit important information and/or fail to be short and to the point.
– Panzercrisis
Apr 12 '16 at 14:03
@cdhowie What if while saying,var foo = new Foo()
,foo
could be of typeFoo
or any of its super classes. How can that be any legal?
– John Strood
Aug 21 '16 at 17:26
3
@Djack It's not legal, and that's not the case.var foo = new Foo();
is the same thing asFoo foo = new Foo();
, which means thatfoo
can contain aFoo
reference, or a reference to an object anyFoo
subtype, not aFoo
supertype.
– cdhowie
Aug 21 '16 at 21:13
add a comment |
It means the data type is derived (implied) from the context.
From http://msdn.microsoft.com/en-us/library/bb383973.aspx
Beginning in Visual C# 3.0, variables
that are declared at method scope can
have an implicit type var. An
implicitly typed local variable is
strongly typed just as if you had
declared the type yourself, but the
compiler determines the type. The
following two declarations of i are
functionally equivalent:
var i = 10; // implicitly typed
int i = 10; //explicitly typed
var
is useful for eliminating keyboard typing and visual noise, e.g.,
MyReallyReallyLongClassName x = new MyReallyReallyLongClassName();
becomes
var x = new MyReallyReallyLongClassName();
but can be overused to the point where readability is sacrificed.
add a comment |
"var" means the compiler will determine the explicit type of the variable, based on usage. For example,
var myVar = new Connection();
would give you a variable of type Connection.
add a comment |
It declares a type based on what is assigned to it in the initialisation.
A simple example is that the code:
var i = 53;
Will examine the type of 53, and essentially rewrite this as:
int i = 53;
Note that while we can have:
long i = 53;
This won't happen with var. Though it can with:
var i = 53l; // i is now a long
Similarly:
var i = null; // not allowed as type can't be inferred.
var j = (string) null; // allowed as the expression (string) null has both type and value.
This can be a minor convenience with complicated types. It is more important with anonymous types:
var i = from x in SomeSource where x.Name.Length > 3 select new x.ID, x.Name;
foreach(var j in i)
Console.WriteLine(j.ID.ToString() + ":" + j.Name);
Here there is no other way of defining i
and j
than using var
as there is no name for the types that they hold.
add a comment |
Did you ever hated to write such variable initializers?
XmlSerializer xmlSerializer = new XmlSerialzer(typeof(int))
So, starting with C# 3.0, you can replace it with
var xmlSerializer = new XmlSerialzer(typeof(int))
One notice: Type is resolved during compilation, so no problems with performance. But Compiler should be able to detect type during build step, so code like var xmlSerializer;
won't compile at all.
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
It means that the type of the local being declared will be inferred by the compiler:
// This statement:
var foo = "bar";
// Is equivalent to this statement:
string foo = "bar";
Notably, var
does not define a variable to be of a dynamic type. So this is NOT legal:
var foo = "bar";
foo = 1; // Compiler error, the foo variable holds strings, not ints
var
has only two uses:
- It requires less typing to declare variables, especially when declaring a variable as a nested generic type.
- It must be used when storing a reference to an object of an anonymous type, because the type name cannot be known in advance:
var foo = new Bar = "bar" ;
You cannot use var
as the type of anything but locals. So you can't use the keyword var
to declare field/property/parameter/return types.
3
This is a good example of what answers need to be like on Stack Exchange. Sometimes people omit important information and/or fail to be short and to the point.
– Panzercrisis
Apr 12 '16 at 14:03
@cdhowie What if while saying,var foo = new Foo()
,foo
could be of typeFoo
or any of its super classes. How can that be any legal?
– John Strood
Aug 21 '16 at 17:26
3
@Djack It's not legal, and that's not the case.var foo = new Foo();
is the same thing asFoo foo = new Foo();
, which means thatfoo
can contain aFoo
reference, or a reference to an object anyFoo
subtype, not aFoo
supertype.
– cdhowie
Aug 21 '16 at 21:13
add a comment |
It means that the type of the local being declared will be inferred by the compiler:
// This statement:
var foo = "bar";
// Is equivalent to this statement:
string foo = "bar";
Notably, var
does not define a variable to be of a dynamic type. So this is NOT legal:
var foo = "bar";
foo = 1; // Compiler error, the foo variable holds strings, not ints
var
has only two uses:
- It requires less typing to declare variables, especially when declaring a variable as a nested generic type.
- It must be used when storing a reference to an object of an anonymous type, because the type name cannot be known in advance:
var foo = new Bar = "bar" ;
You cannot use var
as the type of anything but locals. So you can't use the keyword var
to declare field/property/parameter/return types.
3
This is a good example of what answers need to be like on Stack Exchange. Sometimes people omit important information and/or fail to be short and to the point.
– Panzercrisis
Apr 12 '16 at 14:03
@cdhowie What if while saying,var foo = new Foo()
,foo
could be of typeFoo
or any of its super classes. How can that be any legal?
– John Strood
Aug 21 '16 at 17:26
3
@Djack It's not legal, and that's not the case.var foo = new Foo();
is the same thing asFoo foo = new Foo();
, which means thatfoo
can contain aFoo
reference, or a reference to an object anyFoo
subtype, not aFoo
supertype.
– cdhowie
Aug 21 '16 at 21:13
add a comment |
It means that the type of the local being declared will be inferred by the compiler:
// This statement:
var foo = "bar";
// Is equivalent to this statement:
string foo = "bar";
Notably, var
does not define a variable to be of a dynamic type. So this is NOT legal:
var foo = "bar";
foo = 1; // Compiler error, the foo variable holds strings, not ints
var
has only two uses:
- It requires less typing to declare variables, especially when declaring a variable as a nested generic type.
- It must be used when storing a reference to an object of an anonymous type, because the type name cannot be known in advance:
var foo = new Bar = "bar" ;
You cannot use var
as the type of anything but locals. So you can't use the keyword var
to declare field/property/parameter/return types.
It means that the type of the local being declared will be inferred by the compiler:
// This statement:
var foo = "bar";
// Is equivalent to this statement:
string foo = "bar";
Notably, var
does not define a variable to be of a dynamic type. So this is NOT legal:
var foo = "bar";
foo = 1; // Compiler error, the foo variable holds strings, not ints
var
has only two uses:
- It requires less typing to declare variables, especially when declaring a variable as a nested generic type.
- It must be used when storing a reference to an object of an anonymous type, because the type name cannot be known in advance:
var foo = new Bar = "bar" ;
You cannot use var
as the type of anything but locals. So you can't use the keyword var
to declare field/property/parameter/return types.
edited Nov 29 '10 at 20:03
answered Nov 29 '10 at 19:56
cdhowiecdhowie
111k15216237
111k15216237
3
This is a good example of what answers need to be like on Stack Exchange. Sometimes people omit important information and/or fail to be short and to the point.
– Panzercrisis
Apr 12 '16 at 14:03
@cdhowie What if while saying,var foo = new Foo()
,foo
could be of typeFoo
or any of its super classes. How can that be any legal?
– John Strood
Aug 21 '16 at 17:26
3
@Djack It's not legal, and that's not the case.var foo = new Foo();
is the same thing asFoo foo = new Foo();
, which means thatfoo
can contain aFoo
reference, or a reference to an object anyFoo
subtype, not aFoo
supertype.
– cdhowie
Aug 21 '16 at 21:13
add a comment |
3
This is a good example of what answers need to be like on Stack Exchange. Sometimes people omit important information and/or fail to be short and to the point.
– Panzercrisis
Apr 12 '16 at 14:03
@cdhowie What if while saying,var foo = new Foo()
,foo
could be of typeFoo
or any of its super classes. How can that be any legal?
– John Strood
Aug 21 '16 at 17:26
3
@Djack It's not legal, and that's not the case.var foo = new Foo();
is the same thing asFoo foo = new Foo();
, which means thatfoo
can contain aFoo
reference, or a reference to an object anyFoo
subtype, not aFoo
supertype.
– cdhowie
Aug 21 '16 at 21:13
3
3
This is a good example of what answers need to be like on Stack Exchange. Sometimes people omit important information and/or fail to be short and to the point.
– Panzercrisis
Apr 12 '16 at 14:03
This is a good example of what answers need to be like on Stack Exchange. Sometimes people omit important information and/or fail to be short and to the point.
– Panzercrisis
Apr 12 '16 at 14:03
@cdhowie What if while saying,
var foo = new Foo()
, foo
could be of type Foo
or any of its super classes. How can that be any legal?– John Strood
Aug 21 '16 at 17:26
@cdhowie What if while saying,
var foo = new Foo()
, foo
could be of type Foo
or any of its super classes. How can that be any legal?– John Strood
Aug 21 '16 at 17:26
3
3
@Djack It's not legal, and that's not the case.
var foo = new Foo();
is the same thing as Foo foo = new Foo();
, which means that foo
can contain a Foo
reference, or a reference to an object any Foo
subtype, not a Foo
supertype.– cdhowie
Aug 21 '16 at 21:13
@Djack It's not legal, and that's not the case.
var foo = new Foo();
is the same thing as Foo foo = new Foo();
, which means that foo
can contain a Foo
reference, or a reference to an object any Foo
subtype, not a Foo
supertype.– cdhowie
Aug 21 '16 at 21:13
add a comment |
It means the data type is derived (implied) from the context.
From http://msdn.microsoft.com/en-us/library/bb383973.aspx
Beginning in Visual C# 3.0, variables
that are declared at method scope can
have an implicit type var. An
implicitly typed local variable is
strongly typed just as if you had
declared the type yourself, but the
compiler determines the type. The
following two declarations of i are
functionally equivalent:
var i = 10; // implicitly typed
int i = 10; //explicitly typed
var
is useful for eliminating keyboard typing and visual noise, e.g.,
MyReallyReallyLongClassName x = new MyReallyReallyLongClassName();
becomes
var x = new MyReallyReallyLongClassName();
but can be overused to the point where readability is sacrificed.
add a comment |
It means the data type is derived (implied) from the context.
From http://msdn.microsoft.com/en-us/library/bb383973.aspx
Beginning in Visual C# 3.0, variables
that are declared at method scope can
have an implicit type var. An
implicitly typed local variable is
strongly typed just as if you had
declared the type yourself, but the
compiler determines the type. The
following two declarations of i are
functionally equivalent:
var i = 10; // implicitly typed
int i = 10; //explicitly typed
var
is useful for eliminating keyboard typing and visual noise, e.g.,
MyReallyReallyLongClassName x = new MyReallyReallyLongClassName();
becomes
var x = new MyReallyReallyLongClassName();
but can be overused to the point where readability is sacrificed.
add a comment |
It means the data type is derived (implied) from the context.
From http://msdn.microsoft.com/en-us/library/bb383973.aspx
Beginning in Visual C# 3.0, variables
that are declared at method scope can
have an implicit type var. An
implicitly typed local variable is
strongly typed just as if you had
declared the type yourself, but the
compiler determines the type. The
following two declarations of i are
functionally equivalent:
var i = 10; // implicitly typed
int i = 10; //explicitly typed
var
is useful for eliminating keyboard typing and visual noise, e.g.,
MyReallyReallyLongClassName x = new MyReallyReallyLongClassName();
becomes
var x = new MyReallyReallyLongClassName();
but can be overused to the point where readability is sacrificed.
It means the data type is derived (implied) from the context.
From http://msdn.microsoft.com/en-us/library/bb383973.aspx
Beginning in Visual C# 3.0, variables
that are declared at method scope can
have an implicit type var. An
implicitly typed local variable is
strongly typed just as if you had
declared the type yourself, but the
compiler determines the type. The
following two declarations of i are
functionally equivalent:
var i = 10; // implicitly typed
int i = 10; //explicitly typed
var
is useful for eliminating keyboard typing and visual noise, e.g.,
MyReallyReallyLongClassName x = new MyReallyReallyLongClassName();
becomes
var x = new MyReallyReallyLongClassName();
but can be overused to the point where readability is sacrificed.
edited Nov 29 '10 at 20:00
answered Nov 29 '10 at 19:55
RedFilterRedFilter
136k30243256
136k30243256
add a comment |
add a comment |
"var" means the compiler will determine the explicit type of the variable, based on usage. For example,
var myVar = new Connection();
would give you a variable of type Connection.
add a comment |
"var" means the compiler will determine the explicit type of the variable, based on usage. For example,
var myVar = new Connection();
would give you a variable of type Connection.
add a comment |
"var" means the compiler will determine the explicit type of the variable, based on usage. For example,
var myVar = new Connection();
would give you a variable of type Connection.
"var" means the compiler will determine the explicit type of the variable, based on usage. For example,
var myVar = new Connection();
would give you a variable of type Connection.
answered Nov 29 '10 at 19:56
aaaa bbbbaaaa bbbb
2,12811722
2,12811722
add a comment |
add a comment |
It declares a type based on what is assigned to it in the initialisation.
A simple example is that the code:
var i = 53;
Will examine the type of 53, and essentially rewrite this as:
int i = 53;
Note that while we can have:
long i = 53;
This won't happen with var. Though it can with:
var i = 53l; // i is now a long
Similarly:
var i = null; // not allowed as type can't be inferred.
var j = (string) null; // allowed as the expression (string) null has both type and value.
This can be a minor convenience with complicated types. It is more important with anonymous types:
var i = from x in SomeSource where x.Name.Length > 3 select new x.ID, x.Name;
foreach(var j in i)
Console.WriteLine(j.ID.ToString() + ":" + j.Name);
Here there is no other way of defining i
and j
than using var
as there is no name for the types that they hold.
add a comment |
It declares a type based on what is assigned to it in the initialisation.
A simple example is that the code:
var i = 53;
Will examine the type of 53, and essentially rewrite this as:
int i = 53;
Note that while we can have:
long i = 53;
This won't happen with var. Though it can with:
var i = 53l; // i is now a long
Similarly:
var i = null; // not allowed as type can't be inferred.
var j = (string) null; // allowed as the expression (string) null has both type and value.
This can be a minor convenience with complicated types. It is more important with anonymous types:
var i = from x in SomeSource where x.Name.Length > 3 select new x.ID, x.Name;
foreach(var j in i)
Console.WriteLine(j.ID.ToString() + ":" + j.Name);
Here there is no other way of defining i
and j
than using var
as there is no name for the types that they hold.
add a comment |
It declares a type based on what is assigned to it in the initialisation.
A simple example is that the code:
var i = 53;
Will examine the type of 53, and essentially rewrite this as:
int i = 53;
Note that while we can have:
long i = 53;
This won't happen with var. Though it can with:
var i = 53l; // i is now a long
Similarly:
var i = null; // not allowed as type can't be inferred.
var j = (string) null; // allowed as the expression (string) null has both type and value.
This can be a minor convenience with complicated types. It is more important with anonymous types:
var i = from x in SomeSource where x.Name.Length > 3 select new x.ID, x.Name;
foreach(var j in i)
Console.WriteLine(j.ID.ToString() + ":" + j.Name);
Here there is no other way of defining i
and j
than using var
as there is no name for the types that they hold.
It declares a type based on what is assigned to it in the initialisation.
A simple example is that the code:
var i = 53;
Will examine the type of 53, and essentially rewrite this as:
int i = 53;
Note that while we can have:
long i = 53;
This won't happen with var. Though it can with:
var i = 53l; // i is now a long
Similarly:
var i = null; // not allowed as type can't be inferred.
var j = (string) null; // allowed as the expression (string) null has both type and value.
This can be a minor convenience with complicated types. It is more important with anonymous types:
var i = from x in SomeSource where x.Name.Length > 3 select new x.ID, x.Name;
foreach(var j in i)
Console.WriteLine(j.ID.ToString() + ":" + j.Name);
Here there is no other way of defining i
and j
than using var
as there is no name for the types that they hold.
answered Nov 29 '10 at 20:05
Jon HannaJon Hanna
91.4k9112206
91.4k9112206
add a comment |
add a comment |
Did you ever hated to write such variable initializers?
XmlSerializer xmlSerializer = new XmlSerialzer(typeof(int))
So, starting with C# 3.0, you can replace it with
var xmlSerializer = new XmlSerialzer(typeof(int))
One notice: Type is resolved during compilation, so no problems with performance. But Compiler should be able to detect type during build step, so code like var xmlSerializer;
won't compile at all.
add a comment |
Did you ever hated to write such variable initializers?
XmlSerializer xmlSerializer = new XmlSerialzer(typeof(int))
So, starting with C# 3.0, you can replace it with
var xmlSerializer = new XmlSerialzer(typeof(int))
One notice: Type is resolved during compilation, so no problems with performance. But Compiler should be able to detect type during build step, so code like var xmlSerializer;
won't compile at all.
add a comment |
Did you ever hated to write such variable initializers?
XmlSerializer xmlSerializer = new XmlSerialzer(typeof(int))
So, starting with C# 3.0, you can replace it with
var xmlSerializer = new XmlSerialzer(typeof(int))
One notice: Type is resolved during compilation, so no problems with performance. But Compiler should be able to detect type during build step, so code like var xmlSerializer;
won't compile at all.
Did you ever hated to write such variable initializers?
XmlSerializer xmlSerializer = new XmlSerialzer(typeof(int))
So, starting with C# 3.0, you can replace it with
var xmlSerializer = new XmlSerialzer(typeof(int))
One notice: Type is resolved during compilation, so no problems with performance. But Compiler should be able to detect type during build step, so code like var xmlSerializer;
won't compile at all.
answered Nov 29 '10 at 20:05
The SmallestThe Smallest
4,8592033
4,8592033
add a comment |
add a comment |