13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

212 CHAPTER 8 Cutt<strong>in</strong>g fluff with a smart compilerFigure 8.2 Hover<strong>in</strong>gover the use of animplicitly typed localvariable displays its type.Aga<strong>in</strong>, that’s exactly the same behavior as a normal local variable declaration. Now,there are two reasons for br<strong>in</strong>g<strong>in</strong>g up Visual Studio 2008 <strong>in</strong> this context. The first isthat it’s more evidence of the static typ<strong>in</strong>g <strong>in</strong>volved—the compiler clearly knows thetype of the variable. The second is to po<strong>in</strong>t out that you can easily discover the type<strong>in</strong>volved, even from deep with<strong>in</strong> a method. This will be important when we talk aboutthe pros and cons of us<strong>in</strong>g implicit typ<strong>in</strong>g <strong>in</strong> a m<strong>in</strong>ute. First, though, I ought to mentionsome limitations.8.2.2 Restrictions on implicit typ<strong>in</strong>gYou can’t use implicit typ<strong>in</strong>g for every variable <strong>in</strong> every situation. You can only use itwhen■ The variable be<strong>in</strong>g declared is a local variable.■ The variable is <strong>in</strong>itialized as part of the declaration.■ The <strong>in</strong>itialization expression isn’t a method group or anonymous function 1(without cast<strong>in</strong>g).■ The <strong>in</strong>itialization expression isn’t null.■ Only one variable is declared <strong>in</strong> the statement.■ The type you want the variable to have is the compile-time type of the <strong>in</strong>itializationexpression.The third and fourth po<strong>in</strong>ts are <strong>in</strong>terest<strong>in</strong>g. You can’t writevar starter = delegate() { Console.WriteL<strong>in</strong>e(); }This is because the compiler doesn’t know what type to use. You can writevar starter = (ThreadStart) delegate() { Console.WriteL<strong>in</strong>e(); }but if you’re go<strong>in</strong>g to do that you’d be better off explicitly declar<strong>in</strong>g the variable <strong>in</strong>the first place. The same is true <strong>in</strong> the null case—you could cast the null appropriately,but there’d be no po<strong>in</strong>t. Note that you can use the result of method calls orproperties as the <strong>in</strong>itialization expression—you’re not limited to constants and constructorcalls. For <strong>in</strong>stance, you could usevar args = Environment.CommandL<strong>in</strong>e;In that case args would then be of type str<strong>in</strong>g[]. In fact, <strong>in</strong>itializ<strong>in</strong>g a variable withthe result of a method call is likely to be the most common situation where implicit1The term anonymous function covers both anonymous methods and lambda expressions, which we’ll delve <strong>in</strong>to<strong>in</strong> chapter 9.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!