C# Featurette #2 – Inline Warning Control


Another feature that we added for Whidbey is the ability to selectively disable warnings within your code. For example:


#pragma warning disable 135


  // Disable warning CS135 in this block


#pragma warning restore 135


 

Comments (19)

  1. Jason G says:

    Cool feature. Not sure where I would use it, except maybe to disable warnings about XML documentation not being on a public member for a few autogenerated classes. *cough*XSD.exe*cough*

    I don’t suppose there is a way to selectively choose which exceptions the IDE will "break when thrown" on a per assembly/class/file basis using pragmas is there?

    My example is our collab partners have some code that throws 30-40 FileNotFoundExceptions that I would love to be able to ignore since it’s "handled", but I would still like to break when thrown for that exception with all the rest of our code.

  2. Nic Wise says:

    Good feature, but I think its general use should be discouraged – warnings are GOOD, and you should indever to fix your code, not just block them out….

    🙂

    But, you can’t always 🙂

  3. Thomas Eyde says:

    So when can I disable compiler errors like missing type casts? Those are not really not errors as the compiler will bark at truly incompatible type casts. Note: I am talking about casting, not converting.

  4. Mike Dimmick says:

    I’ll definitely be using this feature for a few cases where I’ll never use some features of a class I’ve declared.

    For example, I have some interfaces which contain events. Some of the implementations of a particular interface will never fire those events, so the compiler generates a warning.

    Now I come to think of it, I probably should have used abstract classes rather than interfaces.

  5. I’d go and put a big ass label on it saying that it is recommended for you everyone to add an #if DEBUG or whatnot to this. There are reasons warnings are thrown.

    For instance:

    #if DEBUG

    #pragma warning disable 135

    #endif

    // Disable warning CS135 in this block

    #if DEBUG

    #pragma warning restore 135

    #endif

    This way, in the release build, someone would _have_ to take notice.

  6. Paul Norton says:

    Why is the syntax different than the same pragma for the C++ compiler?

    The syntax I’m accustomed to looks like this:

    <code>

    #pragma warning(enable : 4244)

    </code>

  7. I suggest that Microsoft could extend the syntax of C# to allow checked and unchecked wherever unsafe is allowed.

  8. Hooray!

    The place I most want to use this is the stupid requirement to have XML comments on members of a public enumeration. For things like:

    /// <summary>

    /// Marital Status

    /// (Do not use as Filing Status)

    /// </summary>

    public enum MaritalStatus

    {

    Single,

    Married,

    Divorced,

    Widowed

    };

    Where we’d get 4 warnings because C#’s compiler wants me to put an XML comment to explain the difference between "Married" and "Divorced", etc..

  9. I think this feature is useful. I am generally against disabling warnings, however, in this case I added to my project (which requires XML documentation) a class that I downloaded from the Internet. The issue is that this class did not provide such documentation. At the meantime, I didn’t want to do a lot of modifications to the class in order to make it easy to get upgrades in the future. In the meantime, what I will do now is to send the author a comment to support provide the XML documentation to avoid this issue.

    Like any other tool, it can be useful when used appropriately, so irrespective of the exact scenarios, I think it makes sense to have this feature available.

  10. Brian says:

    Great! One of my classes has properties whose values are set only through reflection, and the compiler is always giving me "warning CS0649: Field ‘CommandLineParser.ParserTest.PrivateInstanceArg’ is never assigned to, and will always have its default value null.’ In this case the warning is essentially wrong, but the compiler can’t tell that. I’d love to disable those warnings.

  11. MBA says:

    Helpful For MBA Fans.

Skip to main content