Removing Warnings from CSharp Compile Cycle
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?
This code
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
- Making values nullable - October 9th, 2008
- CSharp's Property Shortcuts - October 23rd, 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
- Advanced CSharp - unsafe mode - 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
- Stackalloc in CSharp - February 16th, 2009
- Removing Warnings from CSharp Compile Cycle - March 10th, 2009
- && vs & and | vs ||... What's the difference? - March 16th, 2009
- Advanced CSharp - yield - March 25th, 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
- Dispose, Finalize and SuppressFinalize - July 9th, 2009
- What is .NET’s Object.GetHashCode() Used For? - August 5th, 2009
- ASP.NET Substitution Control - October 22nd, 2009
Other Related Items:
SAFE-LOK CABINET DOOR & DRAWER LOCK Mommy's HelperThe Safe Lok helps prevent access to cabinets and drawers and guards against pinched fingers. The design allows a cabinet drawer or door to open o... Read More >
Warn 38826 Premium Manual Hub KitWARN INNER AXLE SHAFT -- Right, Alloy, 27 Spline, 15.82' Long, Replacing Automatic 3 Hole Cap With Manual, Use Correct Spindle Nut KitLocation: Inner
Style: Premium Manual









