标签云

微信群

扫码加入我们

WeChat QR Code


The advantage is also saving keystrokes, when your method return type is very complex and/or generic (for example MyVeryComplexGenericType<string, KeyValuePair<int, List<object>>>;), you save some space just by using var.

2018年10月21日12分24秒

It has been discussed so many times on SO,here, here, here,here

2018年10月21日12分24秒

Also note that you don't always know the type (if it's an anonymous one for example).

2018年10月22日12分24秒

I'm aware that you only tried to explain the use of var and did not recommend it, but in my opinion these are good examples not to use var. In the first case with 500, it could also be a uint or a short or even a ushort, most developers are not sure which one. In the last case with the variable stopwatch, i would expect a variable of type System.Diagnostics.Stopwatch not a timer, so a hint at the begin of the line would certainly be helpful.

1970年01月01日00分03秒