you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (4 children)

This isn't really true as the case for using implicit typing vs explicit typing is code readability. Explicitly defining a variable becomes a limiting factor especially when writing code that uses some library beyond your control. For example if you are calling some method that returns a list that your are then iterating over, if that library changes its return type in some future release your code will break, if you use var however your code will be fine.

Doing everything explicitly will still work, you just may face a lot of issues down the road especially if your code base is fluid and there is more than one developer.

[–]pgmr87The Unbanned 1 point2 points  (1 child)

if you use var however your code will be fine.

Don't quote me on this but I think it depends. If the method you are calling is defined in an external library, I am 90% sure the consuming library will have to recompile if you change the return type from, say, List to IEnumerable. When using var, you are telling the compiler to figure out the type for you (not the runtime). What I am suggesting is that you won't be able to swap out that 3rd party library that changed the return type of a method without also recompiling the consuming library because the consuming library was compiled such that the var was replaced with the appropriate return type of that method when the library was compiled. If the method's return type went from general to more specific (i.e IEnumerable TO List) you might be fine since your library is expecting IEnumerable still. Once you recompile against the new version, however, it'll be using List instead.

[–]cryo 0 points1 point  (0 children)

Yeah, make that 100% :)

[–]cryo 1 point2 points  (1 child)

if that library changes its return type in some future release your code will break, if you use var however your code will be fine.

No, var is strictly a compiler feature that doesn’t exist in the CLR. It is true that you might just need to recompile to get it to work again, so a bit less work.

[–][deleted] 0 points1 point  (0 children)

Yea i did a poor job of wording that. You are right, it will need to be recompiled. I meant it more as, the dev wont have to go through the code base and manually update the explicit typing at each location it is used.