Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C# not require a semicolon after a class definition?

Tags:

c#

in C++, a semicolon is required to end the class definition. I am wondering why c# does not require it?

like image 800
user705414 Avatar asked May 17 '11 23:05

user705414


People also ask

Why does letter C exist?

Like the letter G, C emerged from the Phoenician letter gimel (centuries later, gimel became the third letter of the Hebrew alphabet). In ancient Rome, as the Latin alphabet was being adapted from the Greek and Etruscan alphabets, G and C became disambiguated by adding a bar to the bottom end of the C.

Does the letter C need to exist?

This is a very important rule considering about 25% of words in our language contain a C.] So why do we need a C? When we combine the C with an H we DO make a unique sound. Without a C we would go to Hurch instead of Church, we would listen to a Hime instead of a Chime, etc.

Is the letter C useless?

But yeah, here are all the letters from most useless to most useful: X, C, Q, Y, W, H, Z, V, B, D, G, P, E, M, L, U, J, R, F, N, K, A, I, T, S, O. I hope you enjoyed this.

Why does C make two sounds?

In the Latin-based orthographies of many European languages, including English, a distinction between hard and soft ⟨c⟩ occurs in which ⟨c⟩ represents two distinct phonemes. The sound of a hard ⟨c⟩ often precedes the non-front vowels ⟨a⟩, ⟨o⟩ and ⟨u⟩, and is that of the voiceless velar stop, /k/ (as in car).


2 Answers

The C++ language permits declaring a variable in the class declaration. Similar to:

class Mumble {
  // etc...
} globalMumble;

The semi-colon is required syntax to let the compiler know whether or not a variable is being declared as well. This syntax is a chronic source of very hard to interpret compile error messages. The worst one is this:

mumble.h:

class Mumble {
  // etc...
}      // <== note: semi-colon forgotten

mumble.cpp:

#include "stdafx.h"
#include "mumble.h"

int main() {
    Mumble* obj = new Mumble;
}

This produces these wonderful error message:

main.cpp(8): error C2628: 'Mumble' followed by 'int' is illegal (did you forget a ';'?)
main.cpp(9): error C3874: return type of 'wmain' should be 'int' instead of 'Mumble'
main.cpp(10): error C2440: 'return' : cannot convert from 'int' to 'Mumble'

Note that all the error messages refer to the .cpp file, not the .h file that contains the mistake. Desperate programmers have lost hours on this, along with large clumps of head hair.

The C# language was designed by highly skilled C++ programmers. Who set out to design a language that avoided these kind of real-life syntax problems. This is evident in many places in the C# syntax. Long story short: C# doesn't permit this kind of C++ syntax, no semi-colon was required to help the compiler.

like image 155
Hans Passant Avatar answered Oct 26 '22 09:10

Hans Passant


Semicolons delimit statements in C#.

Because classes are not statements (and a brace already ends the class definition), a semicolon would be redundant.

like image 26
Robert Harvey Avatar answered Oct 26 '22 09:10

Robert Harvey