0
Slightly random question, but does the ";" character come after everything but the method in C#? I'm assuming that there are exceptions, if someone could elaborate on this please, as well.
When would one use the semicolon in C#?
8 Réponses
+ 2
@Brian,
"So, to clarify. The semicolon ( ; ) would be used everywhere, except after curly braces."
That's not actually what Ajeet means. Ajeet means, you may use semicolons ; almost after every end of a statement except after a code block.
By that, Ajeet does not mean that you may not use a semicolon after every curly brace { }.
You may still use a semicolon after a curly brace as long as it is not a curly brace for code block.
for example:
new int[] { 1, 2, 3 };
you may still put a semicolon after a curly brace { }
+ 1
the semicolon represents the end of a statement in your code. Every statement will end with one, otherwise you will get a compile error. examples:
int x = 45;
x += 4;
Console.WriteLine("Milk");
if (x == 49)
{
Console.WriteLine("it is 49");
}
im still relatively new and not good at explaining but i hope it helps.
+ 1
Once you do actual programming, you'll understand why, and when semicolon (;) is used. Because your program will not run if you have missing semicolons.
Maybe the main reason why your still unfamiliar with the usage of the semicolon is that your still not programming really.
Try C# programming using Visual Studio, once you miss a semicolon, a colored (red) line will appear at the end of your statement indicating that you missed atnd you need to place a semicolon here.
By using Visual Studio, you'll get to know more not only the things about semicolon but you'll also get a deeper understanding of the .NET Framework and the C# Language itself.
+ 1
you have to put ; everywhere except which is having {} code block..
+ 1
Ajeet, Thanks dude.
Erwin, I'm familiar with coding. I was wondering about the syntax of the language itself, however, you are right using VS would be the right way to go as it would tell one with the intelisense function.
Jaylon, Thanks. I get what you're saying there.
So, to clarify. The semicolon ( ; ) would be used everywhere, except after curly braces.
+ 1
@Brian,
Actually, there is no specific rule on how and when to use semicolons.
The usage of semicolon varies per syntax.
example:
- In the systax in For Loop, there is a rule on where to put semicolons.
- In the syntax for array declaration, there is a proper place where to put (;)
- In the syntax for Do-While Loop, there is also a proper place for (;)
But the General Rule on where to place a semicolon, there is no such thing buddy 👍
0
@Erwin, that's what I was wondering.
I should have phrased the question better.
Is there no 'rule' to the semicolon? Syntactically speaking?
0
@Erwin, riiight.. Ok, cool.
Thanks man, that actually answers my question.
Thanks for getting back to me guys, much appreciated.
Do any of you know of any good C# videos or comprehensive books? Similar to cbt nuggets?