One of the things I try to do when I’m working on my CSharp projects is to make sure I don’t have any compile errors or warnings. The reason for removing the errors is probably obvious–if you have an error, your code isn’t going to run at that place. But what about the warnings?
Well, warnings are there because they are potential errors, things you really ought to fix because they could end up causing unexpected results in your program.
So how do we deal with warnings?
The first thing you should do in your code is go through all of your warnings and make sure they aren’t something you couldn’t fix by rearranging your code. Most of the warnings that show up in your code are of this type.
But what about warnings that you can’t remove?
int _x = 1; string s=null; if(_x is object) s = "abc"; if (_x != 1) s = "xyz"; MessageBox.Show(s);
Will produce an error at “if(_x is object)” because value types are always objects. There is no need to have the if statement.
We should remove the conditional statement or replace it with something more meaningful. But let’s suppose that we can’t. How can we prevent the error from showing up?
By using the #pragma warning directive.
int _x = 1; string s=null; #pragma warning disable 183 if(_x is object) s = "abc"; #pragma warning restore 183 if (_x != 1) s = "xyz"; MessageBox.Show(s);
The disable statement will disable a comma-separated list of warnings from showing up in your compiler’s warning list. The restore statement turns them back on.
Unfortunately, the place where I need this feature the most is in some VB.NET code that I’m writing. Currently this feature is not supported in VB.NET, but there is a rumor that it is to be supported in the 2010 release.
Other post in Advanced CSharp
- Two Interfaces. Same Method. Two meanings. - September 29th, 2008
- Readonly variables in CSharp? Really?! - October 29th, 2008
- Dispose with Using - November 10th, 2008
- Delegates in .NET - December 4th, 2008
- Using Sealed in CSharp - December 8th, 2008
- CSharp checked and unchecked - December 11th, 2008
- Unsafe Mode in C# - December 15th, 2008
- Volatile variables and CSharp threads - December 22nd, 2008
- What is the global keyword in CSharp? - December 29th, 2008
- CSharp fixed keyword - January 5th, 2009
- using - There's more there than you are using - February 2nd, 2009
- Removing Warnings from CSharp Compile Cycle - March 10th, 2009
- Just say “No!” to C# Regions? Really?! - April 16th, 2009
- C# “” better than string.Empty? - April 20th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- CSharp ?? Operator - May 18th, 2009
- Using VB.NET From CSharp - July 1st, 2009
- What is .NET’s Object.GetHashCode() Used For? - August 5th, 2009
- ASP.NET Substitution Control - October 22nd, 2009
- Transaction Tracking Typed Datasets Using SqlTransaction - July 20th, 2010
- CSharp's Property Shortcuts - July 17th, 2012
- && vs & and | vs ||... What's the difference? - August 21st, 2012
- Advanced CSharp - yield - November 27th, 2012
- Making values nullable - December 18th, 2012
- Stackalloc in CSharp - January 22nd, 2013
- Dispose, Finalize and SuppressFinalize - June 12th, 2013
- CSharp Numeric Overflows - July 3rd, 2014
- CSharp IDisposable Confusion - July 17th, 2014
- Why Get Certified? - July 24th, 2014
- Running Selenium In Parallel With Any .NET Unit Testing Tool - July 31st, 2014
- Browser Automation in .NET w/ Chromium - July 9th, 2015
- CefSharp Offscreen [Why do I have so many instances of Chromium?] - October 1st, 2015