7

In the C# Coding Conventions, why does Microsoft suggest using implicit typing when the type of the variable is obvious from the right side of the assignment?

I understand it's not necessary to declare it since it is obvious but why the suggestion? Doesn't explicit typing make the code easier to follow?

var str1 = "This is clearly a string.";

vs

string str2 = "This is clearly a string.";

Is there a compile time benefits for this?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
PersyJack
  • 1,736
  • 1
  • 21
  • 32
  • 1
    No, there is no compile time benefits. And yes, explicit typing makes the code easier to follow. However, sometimes when type is complicated (like generic dictionary, or linq query results) it makes code more compact without making it less clear. At the end of the day it's still just a preference. – MarcinJuraszek Sep 24 '14 at 19:53
  • 5
    `When the type of a variable is clear from the context, use var in the declaration.` From the [same link](http://msdn.microsoft.com/en-us/library/ff926074.aspx#code-snippet-6). Personally it is a matter of choice, I would use `string` in your case, I use `var` for things like `var dictionary = new Dictionar();` – Habib Sep 24 '14 at 19:53
  • 1
    @Habib, also with linq statement, as those data types can be a pain to type up-front. – gunr2171 Sep 24 '14 at 19:54
  • 1
    Because typing redundant statements is wasting your time. And if you don't type redundant statements you can save time. – Servy Sep 24 '14 at 19:54
  • personally I think this is a matter of style and preference .. for readability I would prefer the second choice but since you see `" "` quoted text it's obvious by sight that this is also of string type.. – MethodMan Sep 24 '14 at 19:54
  • 4
    I don't know why the down votes. This is a valid question. – Malk Sep 24 '14 at 19:55
  • 2
    Malk I agree but we can't control that – MethodMan Sep 24 '14 at 19:55
  • @Malk No, it's not a valid SO question. – Servy Sep 24 '14 at 19:56
  • @user14200 - you should check http://stackoverflow.com/questions/650919/using-implicitly-typed-local-variables?rq=1 – MarcinJuraszek Sep 24 '14 at 19:56
  • "the guidelines in this topic are used by Microsoft to develop samples and documentation" — bull... blatant lie. – Athari Sep 24 '14 at 19:56
  • @MarcinJuraszek, surprisingly that question is marked as a duplicate, but there is no linked duplicate question :) – Habib Sep 24 '14 at 19:58
  • 1
    @Habib thank you. I've also found this: [http://programmers.stackexchange.com/questions/125262/should-i-encourage-junior-developers-to-use-explicit-or-implicit-typing]. I only considered string, int etc.. but now it makes sense why they suggest it. – PersyJack Sep 24 '14 at 20:05
  • 1
    Sometimes I find SO just crazy. I won't understand and I don't admit discussion about how my answer could get 4 downvotes. The whole downvotes and the comment were both an opinion like my own answer. Type inference isn't a silver bullet, and explicit typing is still useful. I don't cry because declaring a list with `List a = new List()`. I cry if I need to declare something like this `List>` where explicit typing makes code harder to read. – Matías Fidemraizer Sep 24 '14 at 20:16

0 Answers0