Is C# a superset of C in anyway, like Objective-C or C++? Is there a way to compile C online with constructs such compiler flags?
-
Related: http://stackoverflow.com/questions/8586218/is-c-sharp-a-super-set-of-c-just-like-objective-c-and-c – Steve Melnikoff Jul 21 '16 at 09:51
8 Answers
In a word, No.

- 13,835
- 6
- 33
- 40
-
-
1I wouldn't say it's that bad. It's proven a fine way to break with a lot of C's problems that date back to the 80's. – Greg D Jan 26 '09 at 17:24
-
1It's amazing that this answer gets a +18... while others that require time, research, and thought get nowhere near that. It's a shame. – bobwienholt Jan 26 '09 at 18:41
-
Well I guess it goes back KISS :) anyway didn't expect this to get so much attention – Robert Gould Jan 27 '09 at 00:46
-
3@bobwienholt - I agree - I think it's a shame that more good answers aren't upvoted. I think the problem is that the more complicated answers require research to determine they are correct, so it's hard work to upvote properly. This one is easy to upvote because of its simplicity. – Sam Meldrum Jan 27 '09 at 09:43
Not in any meaningful way. It's inspired by C (among others), but they are quite fundamentally different languages.

- 27,223
- 29
- 105
- 125
C# comes from the "c-like" family as far as syntax is concerned, but as far as architecture it feels more like Java (no pointers [OK, as pointed out below, you can use them, but most people don't know or care about that], arrays are bound checked, garbage collected memory, etc).
You can get much of that in C++, but not in the core.

- 16,131
- 4
- 62
- 86
Let's put it like this:
C
andC#
are similar likerm -rf . /
and# rm -rf . /
are similar
In short, no C is not a subset of C#. The look of many control structures base on C. Like, for-loops, switches, while and so on. At the same time, C# forbids potentially dangerous constructs, like falling off a case in a switch when forgetting a break;, or putting an integer as an if condition where a boolean is expected. The above quote means that C# and C can look very similar, but translate to vastly different results. Where C will not prevent you from removing everything from your partition, C# will protect you from doing this by mistake, figuratively spoken.
At another level, C allows you to type-cast pointers to integers, push those around, cast back and then access memory locations that are then stored in that pointer. C will not protect you from accessing memory which isn't allocated by you. You will get a crash - at best. C# - on the other side - will have exceptions that notice you when you do things like accessing object throgh a null-reference.

- 496,577
- 130
- 894
- 1,212
-
1
-
-
1My favorite analogy is "Java and JavaScript are similar like a Car and Carpet are similar." – Braden Best Oct 09 '16 at 02:41
-
1Technically yes... But practically it's pretty close. But C# isn't close to C in much besides syntax. – Jon Adams Jan 26 '09 at 17:34
-
It is if you're willing to accept that C is still its own language and continued to grow in its own right and after if birthed (Bjarne calls it a direct descendent) C++, they parted ways. Bjarne calls it "a better C" but it is it's own language with a different paradigm. – ChiefTwoPencils Jun 20 '13 at 03:53
C# and C are not really related(aside from similar syntax). C# is jitted down to machine code from IL when the program starts. C# doesn't have pointers with the exception of value types on that stack. C# is fully type safe, C is not. C requires explicit memory management where as C# is garbage collected. The list of differences goes on and on...

- 1,544
- 10
- 11
-
C does not require explicit memory management; there are many GC libraries. Boehm is one. – jrockway Jan 26 '09 at 17:09
-
But...natively C does still need explicit memory management, whether you hide it in a library doesn't hide the fact that it's not natively managed. With C# it's burned into the infrastructure from the get go. – Kev Jan 26 '09 at 17:13
C# is very different from C (and also C++ is very different from C). This not because the semanthics, which are at the end not too different, but because of the concepts, the ideas that are behind C#.
When you program in C# you have to think in a different way from what you are used to do if you use C: this is the main difference.

- 143
- 2
- 7
-
1Ok, I'll bite: Suppose I wanted to implement a matrix multiplication or a bubblesort in C#. Suppose also that I think like a C programmer. Will I fail? – Amy B Jan 26 '09 at 17:38
-
I can compile C code within Obj-C or C++ using the appropriate compiler directives. That's my point – Robert Gould Jan 27 '09 at 00:43
C is imperative. C# is object oriented.

- 14,085
- 12
- 59
- 90
-
1The two are not mutually exclusive, and in fact I'd say that both C and C# are imperative `and` object oriented. (See http://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c). And even if one of them were not, that doesn't really answer the question of whether it is a superset! – Stephen Holt Oct 29 '13 at 16:24