C# Empty Statement
while (GetWhitespace(textStream))
;
What is the use of an empty statement
In the given example it is pointless and/or cosmetic.
The empty statement is "useful" in places where a statement is required but you have nothing to do, like
while (condition_with_side_effects) ;
Because of the side effects required, this will not match with most coding guidelines or best practices.
Consider it a leftover from C.
Possible Mistaken Empty Statement - Do Nothing in C#
I recommend you use use a local variable. Whilst this does not remove the need for the !
operator, it's not hidden by a complicated expression:
var Value = StringLoad("WindowState", "");
var isValidWindowState = Enum.TryParse(Value, out WindowState_);
if (!isValidWindowState)
{
WindowState = System.Windows.WindowState.Normal;
}
On top of that it outlines what the Enum.TryParse
is for in this context. Extract Variable is also a well-known refactoring.
As M.kazem Akhgary pointed out, the runtime behaviour of your application is not changed, because it's (most probably) compiled to the same bytecode.
Possible mistaken empty statement warning
Well, your if statement is empty
if (Jtag.activeConnection) ;
So the compiler is asking you to confirm if that's really what you wanted to do.
Normally it should be something like
if (Jtag.activeConnection)
{
// do something
}
C# possible mistaken empty statement
this code basically translates to
MyDisposableClass tmp = new MyDisposableClass().MethodA();
try
{
}
finally
{
if( tmp != null )
tmp.Dispose();
}
Basically you're disposing the result of the call to MethodA
, rather than disposing of the MyDisposableClass
which is the likely intent.
The ;
following the using statement is legal but the warning suggests that you might have added it there by mistake. For example the following code won't compile:
using( var tmp = new MyDisposableClass() );
{
tmp.MethodA();
}
The parser evaluates two entirely separate blocks and is seen by the compiler as if you had typed this:
using( var tmp = new MyDispoableClass() )
{
}
{
tmp.MethodA();
}
It's easy to miss a dangling ;
by eye so the compiler warning is simply suggesting that you probably meant to do something else. There are times when the shorter concise statement is desired and I think the best way to indicate that it is on purpose is to use {}
instead of a ;
.
using( new MyDisposableClass().MethodA() ){}
Note still that this is disposing the result of the call to MethodA - not the MyDisposableClass instance. Your code should actually be written as
using( var tmp = new MyDisposableClass() ){ tmp.MethodA(); }
Why is the IDE giving me a message saying empty statement is redundant after a foreach?
foreach
loop is closed with }
, then next statement is started (from compiler point of view). And immediately closed with your ;
Single ;
is called empty statement and useful when compiler require to have a statement (inside loop or if/else etc) but you don't need any real action there.
From docs:
The empty statement consists of a single semicolon. It does nothing and can be used in places where a statement is required but no action needs to be performed.
That's why you got warning about "empty statement" and not about "extra semicolon". Because you put empty statement where it's not required and not used.
Possible Mistaken Empty Statement C#
Remove those semicolons from the end of your if
statements.
Because of them, you're always running both statements in the curly braces. Your code is equivalent to this:
if(speed < counterpart); // empty 'if' statement, doing nothing even if true
speed += 0.25F;
if(speed > counterpart); // same here
speed += 0.5F;
Why is there no warning about a possible mistaken empty statement in an empty while loop?
There is no inconsistency. CS0642 is not about validating that your code is meaningful, or ever executed, it's only intended to catch some syntactical patterns that might represent a mistake. Compare:
int x = 1;
while (x < 3) {} // no warning
while (x < 3); {} // CS0642
if (x < 3) ; // CS0642
using (new object() as IDisposable) ; // CS0642
using (new object() as IDisposable) {} // no warning
for (; x < 3 ;) ; // empty statement *and* condition is always true, still no warning
Writing empty loops with a single ;
instead of { }
is a common C idiom that programmers brought with them (for better or worse); having these always trigger CS0642 likely produced too many false positives. using
, however, was never a C idiom, so enforcing that empty blocks always be written as { }
seems reasonable. It certainly catches some cases where you could get it wrong:
TextWriter x = null;
using (x) ; // CS0642
x.WriteLine(); // whoops, use of disposed object
Admittedly, this is not a likely pattern, and wrapping even single statements in blocks is always good practice -- even in C.
Incidentally, if it had been up to Eric Lippert, there'd have been no ;
at all, and it would be { }
everywhere:
The empty statement feature is redundant, rarely needed, and
error-prone, and it creates work for the compiler team to implement a
warning telling you not to use it. The feature could simply have been
cut from C# 1.0.
(Source.)
A follow-up question would be why the C# compiler makes no apparent effort to warn about any non-trivial conditions that are always true
or false
, which is a fairly common feature of C compilers. The compiler has some warnings for expressions that are always true
or false
(like CS0464, "Comparing with null of type 'type' always produces 'false'") but not general ones. There's likely a design decision behind this, and I may even have seen Eric Lippert blog about it once -- or maybe I just imagined that one. In any case, getting no warnings there has nothing to do with CS0642.
Make CS0642 C# Possible mistaken empty statement be a compiler error?
You can do this at build time using the
warnaserror
compiler flag. For example:csc /warnaserror:642
Configure the project using Visual Studio. Open project properties, build tab, under Treat warnings as errors select Specific warnings and enter
642
in the box. Note this setting is per build configuration so you could set it forDebug
but notRelease
if you desire.As option 2 but editing the project file directly, add this line inside the
PropertyGroup
for the build configuration you want to change:<WarningsAsErrors>642</WarningsAsErrors>
Related Topics
Wix Service Installer Overrides Service Installer Settings
Create Web Service Proxy in Visual Studio from a Wsdl File
How to Do Logging in C# Without Using 3Rd Party Libraries
Find() VS. Where().Firstordefault()
When Should I Create a New Dbcontext()
Simple Delegate (Delegate) VS. Multicast Delegates
Correct Way to Get the Coredispatcher in a Windows Store App
How to Copy the Contents of a String to the Clipboard in C#
Spinwait VS Sleep Waiting. Which One to Use
What Happens in Beginprocessrequest()
How am I Misusing the Null-Coalescing Operator? Is This Evaluating "Null" Correctly
How to Auto Resize and Adjust Form Controls with Change in Resolution
How to Import from Excel to a Dataset Using Microsoft.Office.Interop.Excel
C# - How to Iterate Through Classes Fields and Set Properties