Welcome to MSDN Blogs Sign in | Join | Help

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

 

Published Monday, April 12, 2004 9:53 PM by ericgu
Filed under: ,

Comments

Monday, April 12, 2004 10:16 PM by Jason G

# re: C# Featurette #2 - Inline Warning Control

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.

Tuesday, April 13, 2004 3:54 AM by Code/Tea/Etc...

# Eric covers another new C# Whidbey (VS 2005) feature

Tuesday, April 13, 2004 1:42 AM by Nic Wise

# re: C# Featurette #2 - Inline Warning Control

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 :)
Tuesday, April 13, 2004 5:16 AM by 中の技術日誌

# re: DataSetのXMLコメント

re: DataSet?XML????
Tuesday, April 13, 2004 2:26 AM by Thomas Eyde

# re: C# Featurette #2 - Inline Warning Control

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.
Tuesday, April 13, 2004 2:27 AM by Mike Dimmick

# re: C# Featurette #2 - Inline Warning Control

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.
Tuesday, April 13, 2004 5:00 AM by Omer van Kloeten

# re: C# Featurette #2 - Inline Warning Control

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.
Tuesday, April 13, 2004 10:32 AM by Paul Norton

# re: C# Featurette #2 - Inline Warning Control

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>

Wednesday, April 14, 2004 10:55 AM by Something Clever

# C# Featurette Suggestion

I suggest that Microsoft could extend the syntax of C# to allow checked and unchecked wherever unsafe is allowed.
Wednesday, April 14, 2004 3:59 PM by Christian Nagel's OneNotes

# Whidbey C# Featurette

Thursday, April 15, 2004 8:30 AM by Sam Gentile's Blog

# New and Notable 45

Wednesday, April 21, 2004 1:12 PM by Clinton Pierce

# re: C# Featurette #2 - Inline Warning Control

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..
Monday, May 17, 2004 10:46 PM by Victor Boctor

# re: C# Featurette #2 - Inline Warning Control

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.
Thursday, May 20, 2004 4:40 PM by Brian

# re: C# Featurette #2 - Inline Warning Control

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.
Saturday, December 18, 2004 10:57 AM by MBA

# MBA

Helpful For MBA Fans.

# Eric Gunnerson s C Compendium C Featurette 2 Inline Warning Control | Green Tea Fat Burner

# Eric Gunnerson s C Compendium C Featurette 2 Inline Warning Control | Quick Diets

# Eric Gunnerson s C Compendium C Featurette 2 Inline Warning Control | storage bench

New Comments to this post are disabled
 
Page view tracker