Can we use 'var' in this scenario
Can we use var twice in the function. e.g
var varname= sometype;
if(true)
{
varname= type1;
}
else
{
varname=type2;
}
If this is not possible I would say t开发者_C百科his is a limitation of var.
Well that's only using var
itself a single time - but so long as you have an expression to initialize it with, it's fine. (type1
and type2
will need to be convertible to the type of sometype
though.)
What you can't do is this:
var x; // No initialization expression
if (condition)
{
x = firstExpression;
}
else
{
x = secondExpression;
}
There has to be an initialization expression (which can't be an anonymous function or the null literal). If you want this sort of thing, you could potentially use the conditional operator:
var x = condition ? firstExpression : secondExpression;
Yes, you can use var
in this instance, but only sometype
is of the same type as type1
and type2
.
If type1
and type2
are in fact different types, you can still use var
if and only if sometype
is a superclass of both type1
and type2
.
If you don't know at compile time what type1
and type2
are, you will need to use dynamic
instead of var
. If you DO know that both type1
and type2
implement the same interface, use the interface in the declaration instead of var
.
The in-line assignment will define the type of var
; if the type1
and type2
are compatible with sometype
that'll work, but the first assignment is pointless. You could perhaps cast a null
if it is a ref-type; this will essentially a no-op:
var varname= (sometype)null;
But by that point, you might as well just use no assignment:
sometype varname;
Arguably a better layout would be
var name = condition ? expr1 : expr2;
Note that if there isn't an obvious same type (perhaps they are different subclasses of a common base-type), you may need to help it decide:
var name = condition ? (Foo)expr1 : expr2;
If the types are unrelated, this is not an appropriate use of var
; you may find object
or dynamic
more suitable.
Your example is confusing. You don't assign types to variables, you assign values. If you say var varname=3;
then varname
is an int
. You are then constrained to only assign ints (and values that convert to ints) to it, just as if you had said int varname=3;
- there's no difference.
This is not possible. var
is typesafe, meaning its type will be inferred from the assignment. So in your example, the var
is of type sometype. Assigning a different type to it will not be possible (as long as there is no conversion possible between them).
Using object
instead of var
will work in this scenario.
It's possible as long as you have initialization syntax in place. Additionally, it is good practice to use var for reference types and type for value types
The var type in C# requires initialization to a type. That type is determined and enforced at compile time. When the compiler determines that the type will vary, it will give it an anonymous type (see http://msdn.microsoft.com/en-us/library/bb397696.aspx).
The anonymous type can only be cast to an object type. So the short answer is no, you cannot use a var to refer to more than one type. Even object which can contain various types is itself a type and a var that is given that type cannot be changed to another. In this sense the var keyword allows you to be flexible and yet still strongly typed.
If you have an array of items of various types, the compiler implement the anon type to the var and it will essentially act as an object. A decent explanation can be found at: http://msdn.microsoft.com/en-us/library/bb384061.aspx.
精彩评论