<p><a href="http://resharper.blogspot.com/2008/03/varification-using-implicitly-typed.html" rel="noreferrer">Resharper</a> certainly thinks so, and out of the box it will nag you to convert</p>

<pre><code>Dooberry dooberry = new Dooberry();
</code></pre>

<p>to</p>

<pre><code>var dooberry = new Dooberry();
</code></pre>

<p>Is that really considered the best style?</p>

[{'answer_id': 17041, 'author': 'John Sheehan', 'author_id': 1786, 'author_profile': 'https://Stackoverflow.com/users/1786', 'pm_score': 2, 'selected': False, 'text': "<p>I have a feeling this will be one of the most popular questions asked over time on Stack Overflow. It boils down to preference. Whatever you think is more readable. I prefer var when the type is defined on the right side because it is terser. When I'm assigning a variable from a method call, I use the explicit type declaration.</p>\n"}, {'answer_id': 17042, 'author': 'Jon Galloway', 'author_id': 5, 'author_profile': 'https://Stackoverflow.com/users/5', 'pm_score': 6, 'selected': True, 'text': '<p>It\'s of course a matter of style, but I agree with Dare: <a href="http://www.25hoursaday.com/weblog/2008/05/21/C30ImplicitTypeDeclarationsToVarOrNotToVar.aspx" rel="noreferrer">C# 3.0 Implicit Type Declarations: To var or not to var?</a>. I think using var instead of an explicit type makes your code less readable.In the following code:</p>\n\n<pre><code>var result = GetUserID();\n</code></pre>\n\n<p>What is result? An int, a string, a GUID? Yes, it matters, and no, I shouldn\'t have to dig through the code to know. It\'s especially annoying in code samples.</p>\n\n<p>Jeff wrote a post on this, saying <a href="http://blog.codinghorror.com/department-of-declaration-redundancy-department/" rel="noreferrer">he favors var</a>. But that guy\'s crazy!</p>\n\n<p>I\'m seeing a pattern for stackoverflow success: dig up old CodingHorror posts and (Jeopardy style) phrase them in terms of a question.</p>\n'}, {'answer_id': 17043, 'author': 'OJ.', 'author_id': 611, 'author_profile': 'https://Stackoverflow.com/users/611', 'pm_score': 0, 'selected': False, 'text': '<p>"Best style" is subjective and varies depending on context.</p>\n\n<p>Sometimes it is way easier to use \'var\' instead of typing out some hugely long class name, or if you\'re unsure of the return type of a given function. I find I use \'var\' more when mucking about with Linq, or in for loop declarations.</p>\n\n<p>Other times, using the full class name is more helpful as it documents the code better than \'var\' does.</p>\n\n<p>I feel that it\'s up to the developer to make the decision. There is no silver bullet. No "one true way".</p>\n\n<p>Cheers!</p>\n'}, {'answer_id': 17044, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 0, 'selected': False, 'text': "<p>No not <strong>always</strong> but I would go as far as to say a lot of the time. Type declarations aren't much more useful than Hungarian notation ever was. You still have the same problem that types are subject to change and as much as refactoring tools are helpful for that it's not ideal compared to not having to change where a type is specified except in a single place, which follows the Don't Repeat Yourself principle.</p>\n<p>Any single line statement where a type's name can be specified for both a variable and its value should definitely use var, especially when it's a long <code>Generic&lt;OtherGeneric&lt; T,U,V&gt;, Dictionary&lt; X, Y&gt;&gt;&gt;</code></p>\n"}, {'answer_id': 17045, 'author': 'Ryan Sampson', 'author_id': 1375, 'author_profile': 'https://Stackoverflow.com/users/1375', 'pm_score': 2, 'selected': False, 'text': '<p>There was a good discussion on this @ <a href="http://www.codinghorror.com/blog/archives/001136.html" rel="nofollow noreferrer">Coding Horror</a></p>\n\n<p>Personally I try to keep its use to a minimum, I have found it hurts readability especially when assigning a variable from a method call.</p>\n'}, {'answer_id': 17100, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 2, 'selected': False, 'text': "<p>@jongalloway - var doesn't necessarily make your code more unreadable.</p>\n<pre><code>var myvariable = DateTime.Now\nDateTime myvariable = DateTime.Now;\n</code></pre>\n<p>The first is just as readable as the second and requires less work</p>\n<pre><code>var myvariable = ResultFromMethod();\n</code></pre>\n<p>here, you have a point, var could make the code less readable. I like var because if I change a decimal to a double, I don't have to go change it in a bunch of places (and don't say refactor, sometimes I forget, just let me var!)</p>\n<p><strong>EDIT:</strong> just read the article, I agree. lol.</p>\n"}, {'answer_id': 17126, 'author': 'John Richardson', 'author_id': 887, 'author_profile': 'https://Stackoverflow.com/users/887', 'pm_score': 1, 'selected': False, 'text': "<p>One of the advantages of a tool like ReSharper is that you can write the code however you like and have it reformat to something more maintainable afterward. I have R# set to always reformat such that the actual type in use is visible, however, when writing code I nearly always type 'var'.</p>\n<p>Good tools let you have the best of both worlds.</p>\n<p>John.</p>\n"}, {'answer_id': 17313, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 0, 'selected': False, 'text': '<p>There\'s a <a href="http://msdn.microsoft.com/en-us/library/bb384061.aspx" rel="nofollow noreferrer">really good MSDN article</a> on this topic and it outlines some cases where you can\'t use var:</p>\n<p>The following restrictions apply to implicitly-typed variable declarations:</p>\n<blockquote>\n<ul>\n<li>var can only be used when a local variable is declared and initialized\nin the same statement; the variable\ncannot be initialized to null, or to a\nmethod group or an anonymous function.</li>\n<li>var cannot be used on fields at class scope.</li>\n<li>Variables declared by using var cannot be used in the initialization\nexpression. In other words, this\nexpression is legal: int i = (i = 20);\nbut this expression produces a\ncompile-time error: var i = (i = 20);</li>\n<li>Multiple implicitly-typed variables cannot be initialized in the same\nstatement.</li>\n<li>If a type named var is in scope, then the var keyword will resolve to\nthat type name and will not be treated\nas part of an implicitly typed local\nvariable declaration.</li>\n</ul>\n</blockquote>\n<p>I would recommend checking it out to understand the full implications of using var in your code.</p>\n'}, {'answer_id': 17347, 'author': 'sieben', 'author_id': 1147, 'author_profile': 'https://Stackoverflow.com/users/1147', 'pm_score': 4, 'selected': False, 'text': '<p>I use it only when it\'s clearly obvious what var is.</p>\n\n<p>clear to me:</p>\n\n<pre><code>XmlNodeList itemList = rssNode.SelectNodes("item");\nvar rssItems = new RssItem[itemList.Count];\n</code></pre>\n\n<p>not clear to me:</p>\n\n<pre><code>var itemList = rssNode.SelectNodes("item");\nvar rssItems = new RssItem[itemList.Count];\n</code></pre>\n'}, {'answer_id': 18091, 'author': 'serg10', 'author_id': 1853, 'author_profile': 'https://Stackoverflow.com/users/1853', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>I'm seeing a pattern for stackoverflow\n  success: dig up old CodingHorror posts\n  and (Jeopardy style) phrase them in\n  terms of a question.</p>\n</blockquote>\n\n<p>I plead innocent!  But you're right, this seemed to be a relatively popular little question.</p>\n"}, {'answer_id': 111475, 'author': 'Greg Beech', 'author_id': 13552, 'author_profile': 'https://Stackoverflow.com/users/13552', 'pm_score': 3, 'selected': False, 'text': '<p>The best summary of the answer I\'ve seen to this is <a href="http://csharpindepth.com/ViewNote.aspx?NoteID=61" rel="nofollow noreferrer">Eric Lippert\'s comment</a>, which essentially says you should use the concrete type if it\'s important what the type is, but not to otherwise. Essentially type information should be reserved for places where the type is important.</p>\n<p>The standard at my company is to use var everywhere, which we came to after <a href="http://gregbeech.com/blogs/tech/archive/2008/03/24/to-var-or-not-to-var-implicit-typing-is-the-question.aspx" rel="nofollow noreferrer">reading various recommendations and then spending some time trying it out to see whether the lack of annotated type information was a help or a hindrance</a>. We felt it was a help.</p>\n<p>Most of the recommendations people have linked to (e.g. Dare\'s one) are recommendations made by people who have never tried coding using var instead of the concrete type. This makes the recommendations all but worthless because they aren\'t speaking from experience, they\'re merely extrapolating.</p>\n<p>The best advice I can give you is to try it for yourself, and see what works for you and your team.</p>\n'}, {'answer_id': 660829, 'author': 'user79829', 'author_id': 79829, 'author_profile': 'https://Stackoverflow.com/users/79829', 'pm_score': 1, 'selected': False, 'text': "<p>It only make sense, when you don't know the type in advance.</p>\n"}, {'answer_id': 68091505, 'author': 'Misha Zaslavsky', 'author_id': 2667173, 'author_profile': 'https://Stackoverflow.com/users/2667173', 'pm_score': 1, 'selected': False, 'text': '<p>In <code>C# 9.0</code> there is a new way to initialize a class by <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/target-typed-new" rel="nofollow noreferrer">Target-typed new expressions</a>.</p>\n<p>You can initialize the class like this:</p>\n<pre><code>Dooberry dooberry = new();\n</code></pre>\n<p>Personally, I like it more than using a <code>var</code> and it is more readable for me.</p>\n<p>Regarding calling a method I think it is up to you. Personally, I prefer to specify the type because I think it is more readable this way:</p>\n<pre><code>Dooberry dooberry = GetDooberry();\n</code></pre>\n<p>In some cases, it is very clear what the type is, in this case, I use var:</p>\n<pre><code>var now = DateTime.Now;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/17032', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1853/']