I am new to C# 3.0 var type. Here I have a question about this type. Take the following simple codes in a library as example:
public class MyClass {
public var Fn(var inValue)
{
if ( inValue < 0 )
{
return 1.0;
}
else
{
return inValue;
}
}
}
I think the parameter is an anonymous type. If I pass in a float value, then the Fn should return a float type. If a double value type is passed in, will the Fn return a double type? How about an integer value type as input value?
Actually, I would like to use var type with this function/method to get different return types with various input types dynamically. I am not sure if this usage is correct or not?
You can’t use
varfor return values or parameter types (or fields). You can only use it for local variables.Eric Lippert has a blog post about why you can’t use it for fields. I’m not sure if there’s a similar one for return values and parameter types. Parameter types certainly doesn’t make much sense – where could the compiler infer the type from? Just what methods you try to call on the parameters? (Actually that’s pretty much what F# does, but C# is more conservative.)
Don’t forget that
varis strictly static typing – it’s just a way of getting the compiler to infer the static type for you. It’s still just a single type, exactly as if you’d typed the name into the code. (Except of course with anonymous types you can’t do that, which is one motivation for the feature.)EDIT: For more details on
var, you can download chapter 8 of C# in Depth for free at Manning’s site – this includes the section onvar. Obviously I hope you’ll then want to buy the book, but there’s no pressure 🙂EDIT: To address your actual aim, you can very nearly implement all of this with a generic method:
As shown in the listing, the tricky bit is working out what “1” means for an arbitrary type. You could hard code a set of values, but it’s a bit ugly:
Icky – but it’ll work. Then you can write:
etc