13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Implicit typ<strong>in</strong>g of local variables213typ<strong>in</strong>g is used, as part of LINQ. We’ll see all that later on—just bear it <strong>in</strong> m<strong>in</strong>d as theexamples progress.It’s also worth not<strong>in</strong>g that you are allowed to use implicit typ<strong>in</strong>g for the local variablesdeclared <strong>in</strong> the first part of a us<strong>in</strong>g, for, or foreach statement. For example, thefollow<strong>in</strong>g are all valid (with appropriate bodies, of course):for (var i = 0; i < 10; i++)us<strong>in</strong>g (var x = File.OpenText("test.dat"))foreach (var s <strong>in</strong> Environment.CommandL<strong>in</strong>e)The variables <strong>in</strong> question would end up with types of <strong>in</strong>t, StreamReader and str<strong>in</strong>g,respectively. Of course, just because you can do this doesn’t mean you should. Let’shave a look at the reasons for and aga<strong>in</strong>st us<strong>in</strong>g implicit typ<strong>in</strong>g.8.2.3 Pros and cons of implicit typ<strong>in</strong>gThe question of when it’s a good idea to use implicit typ<strong>in</strong>g is the cause of an awful lotof community discussion. Views range from “everywhere” to “nowhere” with plenty ofmore balanced approaches between the two. We’ll see <strong>in</strong> section 8.5 that <strong>in</strong> order touse another of <strong>C#</strong> 3’s features—anonymous types—you’ve often got to use implicit typ<strong>in</strong>g.You could avoid anonymous types as well, of course, but that’s throw<strong>in</strong>g the babyout with the bathwater.The ma<strong>in</strong> reason for us<strong>in</strong>g implicit typ<strong>in</strong>g (leav<strong>in</strong>g anonymous types aside for themoment) is that it reduces not only the number of keystrokes required to enter thecode, but also the amount of code on the screen. In particular, when generics are<strong>in</strong>volved the type names can get very long. Figures 8.1 and 8.2 used a type of Dictionary, which is 33 characters. By the time you’ve got that twice ona l<strong>in</strong>e (once for the declaration and once for the <strong>in</strong>itialization), you end up with a massivel<strong>in</strong>e just for declar<strong>in</strong>g and <strong>in</strong>itializ<strong>in</strong>g a s<strong>in</strong>gle variable! An alternative is to use analias, but that puts the “real” type <strong>in</strong>volved a long way (conceptually at least) from thecode that uses it.When read<strong>in</strong>g the code, there’s no po<strong>in</strong>t <strong>in</strong> see<strong>in</strong>g the same long type name twiceon the same l<strong>in</strong>e when it’s obvious that they should be the same. If the declaration isn’tvisible on the screen, you’re <strong>in</strong> the same boat whether implicit typ<strong>in</strong>g was used or not(all the ways you’d use to f<strong>in</strong>d out the variable type are still valid) and if it is visible, theexpression used to <strong>in</strong>itialize the variable tells you the type anyway.All of this sounds good, so what are the arguments aga<strong>in</strong>st implicit typ<strong>in</strong>g? Paradoxicallyenough, readability is the most important one, despite also be<strong>in</strong>g an argument<strong>in</strong> favor of implicit typ<strong>in</strong>g! By not be<strong>in</strong>g explicit about what type of variableyou’re declar<strong>in</strong>g, you may be mak<strong>in</strong>g it harder to work it out just by read<strong>in</strong>g the code.It breaks the “state what are we declar<strong>in</strong>g, then what value it will start off with” m<strong>in</strong>dsetthat keeps the declaration and the <strong>in</strong>itialization quite separate. To what extentthat’s an issue depends on both the reader and the <strong>in</strong>itialization expression <strong>in</strong>volved.If you’re explicitly call<strong>in</strong>g a constructor, it’s always go<strong>in</strong>g to be pretty obvious whattype you’re creat<strong>in</strong>g. If you’re call<strong>in</strong>g a method or us<strong>in</strong>g a property, it depends on howLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!