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.

198 CHAPTER 7 Conclud<strong>in</strong>g <strong>C#</strong> 2: the f<strong>in</strong>al featuresis never read from or written to. It’s almost always go<strong>in</strong>g to be useless… unless we happento know that it will be used by reflection. List<strong>in</strong>g 7.9 is a complete class demonstrat<strong>in</strong>gthis.List<strong>in</strong>g 7.9Class conta<strong>in</strong><strong>in</strong>g an unused fieldpublic class FieldUsedOnlyByReflection{<strong>in</strong>t x;}If you try to compile list<strong>in</strong>g 7.9, you’ll get a warn<strong>in</strong>g message like this:FieldUsedOnlyByReflection.cs(3,9): warn<strong>in</strong>g CS0169:The private field 'FieldUsedOnlyByReflection.x' is never usedThat’s the output from the command-l<strong>in</strong>e compiler. In the Error List w<strong>in</strong>dow of VisualStudio, you can see the same <strong>in</strong>formation (plus the project it’s <strong>in</strong>) except that you don’tget the warn<strong>in</strong>g number (CS0169). To f<strong>in</strong>d out the number, you need to either selectthe warn<strong>in</strong>g and br<strong>in</strong>g up the help related to it, or look <strong>in</strong> the Output w<strong>in</strong>dow, wherethe full text is shown. We need the number <strong>in</strong> order to make the code compile withoutwarn<strong>in</strong>gs, as shown <strong>in</strong> list<strong>in</strong>g 7.10.List<strong>in</strong>g 7.10Disabl<strong>in</strong>g (and restor<strong>in</strong>g) warn<strong>in</strong>g CS0169public class FieldUsedOnlyByReflection{#pragma warn<strong>in</strong>g disable 0169<strong>in</strong>t x;#pragma warn<strong>in</strong>g restore 0169}List<strong>in</strong>g 7.10 is self-explanatory—the first pragma disables the particular warn<strong>in</strong>g we’re<strong>in</strong>terested <strong>in</strong>, and the second one restores it. It’s good practice to disable warn<strong>in</strong>gs foras short a space as you can, so that you don’t miss any warn<strong>in</strong>gs you genu<strong>in</strong>ely ought tofix. If you want to disable or restore multiple warn<strong>in</strong>gs <strong>in</strong> a s<strong>in</strong>gle l<strong>in</strong>e, just use acomma-separated list of warn<strong>in</strong>g numbers. If you don’t specify any warn<strong>in</strong>g numbersat all, the result is to disable or restore all warn<strong>in</strong>gs <strong>in</strong> one fell swoop—but that’s a badidea <strong>in</strong> almost every imag<strong>in</strong>able scenario.7.5.2 Checksum pragmasYou’re very unlikely to need the second form of pragma recognized by the Microsoftcompiler. It supports the debugger by allow<strong>in</strong>g it to check that it’s found the rightsource file. Normally when a <strong>C#</strong> file is compiled, the compiler generates a checksumfrom the file and <strong>in</strong>cludes it <strong>in</strong> the debugg<strong>in</strong>g <strong>in</strong>formation. When the debugger needsto locate a source file and f<strong>in</strong>ds multiple potential matches, it can generate the checksumitself for each of the candidate files and see which is correct.Now, when an ASP.NET page is converted <strong>in</strong>to <strong>C#</strong>, the generated file is what the <strong>C#</strong>compiler sees. The generator calculates the checksum of the .aspx page, and uses aLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!