Does C# Allow Double Semicolon ; ; If So, Are There Any Special Ways

Does C# allow double semicolon ; ; if so, are there any special ways?

No, a double semi-colon is never required. I'm slightly surprised that the compiler doesn't complain that it's an unreachable statement, but apparently it's legal. It won't do any harm, but equally it's not a good idea.

Double semi colon ( ; ; ) allowed in C#.net?

Because ; is just an empty statement in other words null statement. It's usually useless but completely valid.

This is used in loops where you want to perform something repeatedly but do not want to add a loop body.As shown in the below example:

for ( i = 0; i < 10; line[i++] = 0 )
;

In your case however, first semicolon is the end of the statement, second one is the empty statement.Which is useless and will be ignored by the compiler.

Why are multiple semicolons allowed?

That's because the semicolon, when used alone, represents the empty statement.

The documentation says:

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.

And provides the following example:

void ProcessMessages()
{
while (ProcessMessage())
; // Statement needed here.
}

Of course, you can execute as many empty statements as you want in sequence, and nothing will happen.

Better way to add spaces between double semicolons

You can try to Split string into the parts, then replace empty entries with spaces using Select (it requires using System.Linq;) and Join the entries back

var str = "A;B;;;;C";
var parts = str.Split(';').Select(p => string.IsNullOrEmpty(p) ? " " : p);

var result = string.Join(";", parts);

The output will be the following A;B; ; ; ;C

Benchmark result in comparison with OP code and Regex solution:

Sample Image

What is the clear and more elegant is up to you decision.
Benchmark code for the reference is below

[SimpleJob]
public class Benchmark
{
string input= "A;B;;;;C";

[Benchmark]
public string SplitJoinTest()
{
var parts = input.Split(';').Select(p => string.IsNullOrEmpty(p) ? " " : p);
return string.Join(";", parts);
}

[Benchmark]
public string DoubleReplaceTest()
{
return input.Replace(";;", "; ;").Replace(";;", "; ;");
}

[Benchmark]
public string RegexTest()
{
return Regex.Replace(input, ";(?=;)", "; ");
}
}

What does C# syntax ;; mean?

; means empty statement. It does nothing.

In some cases (for example robotic programming), it is useful to instruct the program to continuously pool environment condition and do nothing until the condition fulfilled.

Example

// turn motor on so robot is going forward
TurnMotorOn();

// Do nothing (means motor is still on and robot is still going forward) until there is obstacle in front of the robot.
while(!ThereIsObstacle()) ;

// turn motor off so robot is stopped.
TurnMotorOff();

When do you put double semicolons in F#?

In the non-interactive F# code that's not supposed to be compatible with OCaml, you shouldn't need to ever need double semicolon. In the OCaml compatible mode, you would use it at the end of a top-level function declaration (In the recent versions, you can switch to this mode by using files with .ml extension or by adding #light "off" to the top).

If you're using the command-line fsi.exe tool or F# Interactive in Visual Studio then you'd use ;; to end the current input for F#.

When I'm posting code samples here at StackOverflow (and in the code samples from my book), I use ;; in the listing when I also want to show the result of evaluating the expression in F# interactive:

  • Listing from F# interactive

    > "Hello" + " world!";;
    val it : string = "Hello world!"
    > 1 + 2;;
    val it : int = 3
  • Standard F# source code

    let n = 1 + 2
    printf "Hello world!"

Sometimes it is also useful to show the output as part of the listing, so I find this notation quite useful, but I never explained it anywhere, so it's great that you asked!

Why C# allow this fun? ; ; ; ;

Because semicolon ; is a valid Empty Statement

8.3 The empty statement

An empty-statement does nothing.

empty-statement:

;

An empty statement is used when there are no operations to perform in
a context where a statement is required.

Execution of an empty statement simply transfers control to the end
point of the statement. Thus, the end point of an empty statement is
reachable if the empty statement is reachable.

Also see: Statements (C# Programming Guide) - MSDN

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.

EDIT:

webClient.DownloadFile("http://www.who.int/inf-new/dnldtxt/introductions.zip", ;;;;;;;;;;;;;;;;"");

But Why error shows in this line ?

well ; semicolon is a valid empty statement but that doesn't mean that you can place that anywhere in your code. DownloadFile method expects two parameters , would you place any statement in it like:

webClient.DownloadFile("http://www.who.int/inf-new/dnldtxt/introductions.zip",
Console.Write("Some Text");, "");

Where Console.Write is a valid statement itself, but it can't be used for parameter.

Why this code compiles:

private void install()
{
http://www.stackoverflow.com
return;
}

Because it is treating http: as a Labeled statements and anything after the colon is considered as part of label text. The above code will produce a warning

This label has not been referenced

C# - escaping semicolon and double quotes

Use:

string s = "sometext,sometext".Replace("," , "\";\"");

Both need to be char or string.



Related Topics



Leave a reply



Submit