Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does implicit typing make code clearer?
    text
    copied!<p>In a book I'm reading it states the implicit typing makes the following code clearer than if you didn't use the <code>var</code> keyword:</p> <pre><code>var words = new[] { "a", "b", null, "d" }; foreach (var item in words) { Console.WriteLine(item); } </code></pre> <p>It seems to me that the opposite is true: if you used <code>string</code> instead, then readers of the code would immediately know it was a string in the foreach loop, instead of having to look up in the code where the variable is defined.</p> <p><strong>How does implicit typing make the above code clearer?</strong></p> <h1>Addendum</h1> <p>The book is <a href="http://www.amazon.de/3-0-Die-Neuerungen-schnell-kompakt/dp/3939084581" rel="nofollow noreferrer">C # 3.0 - Die Neuerungen. schnell + kompakt</a> which is in German, the actual text is:</p> <blockquote> <p>Das Schluesselwort var kann auch beim Durchlaufen von foreach-Schleifen verwendet werden, um somit den Code uebersichtlicher und einfacher zu gestalten. Besonders bei komplexen Typen kann man auf diese Art und Weise Programmierfehler verhindern.</p> </blockquote> <p>here's my translation:</p> <blockquote> <p>The var keyword can also be used when iterating through foreach loops, thus making the code easier and simpler to create. Especially when using complex types, this can prevent programming errors.</p> </blockquote> <p>Ok, reading it more closely now he actually states that <code>var</code> in a foreach loop makes the code <em>easier to create</em> but not necessarily easier to read.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload