C# and XML
(Page 1 of 4 )
In this conclusion to a ten-part series on C#, we start with attributes, but you will also learn about XML documentation and XML tags. This article is excerpted from chapter four of
C# 3.0 in a Nutshell, Third Edition, A Desktop Quick Reference, written by Joseph Albahari and Ben Albahari (O'Reilly; ISBN: 0596527578). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Conditional Attributes
An attribute decorated with the Conditional attribute will be compiled only if a given preprocessor symbol is present. For example:
// file1.cs
#define DEBUG
using System;
using System.Diagnostics;
[Conditional("DEBUG")]
public class TestAttribute : Attribute {}
// file2.cs
#define DEBUG
[Test]
class Foo
{
[Test]
private string s;
}
The compiler will not incorporate the[Test]attributes if the DEBUG symbol is in scope for file2.cs.
Pragma Warning
The compiler generates a warning when it spots something in your code that seems unintentional. Unlike errors, warnings don’t ordinarily prevent your application from compiling.
Compiler warnings can be extremely valuable in spotting bugs. Their usefulness, however, is undermined when you get an excessive number of them. In a large application, maintaining a good signal-to-noise ratio is essential if the “real” warnings are to get noticed.
To this effect, the compiler allows you to selectively suppress warnings with the#pragma warningdirective. In this example, we instruct the compiler not to warn us about the fieldMessagenot being used:
public class Foo
{
static void Main() { }
#pragma warning disable 414
static string Message = "Hello";
#pragma warning restore 414
}
Omitting the number in the#pragma warningdirective disables or restores all warning codes.
If you are thorough in applying this directive, you can compile with the/warnaserrorswitch—this tells the compiler to treat any residual warnings as errors.
Next: XML Documentation >>
More C# Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of C# 3.0 in a Nutshell, Third Edition, A Desktop Quick Reference, written by Joseph Albahari and Ben Albahari (O'Reilly; ISBN: 0596527578). Check it out today at your favorite bookstore. Buy this book now.
|
|