views:

539

answers:

7

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?

+28  A: 

In a word, No.

Sam Meldrum
That's what I thought, too bad
Robert Gould
I 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
It'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
Well I guess it goes back KISS :) anyway didn't expect this to get so much attention
Robert Gould
@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
+8  A: 

Not in any meaningful way. It's inspired by C (among others), but they are quite fundamentally different languages.

frou
+5  A: 

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.

Godeke
there are pointers if you want to use them
frou
+4  A: 

Let's put it like this:

C and C# are similar like rm -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.

Johannes Schaub - litb
Or Java and Javascript apparently :)
Robert Gould
Robert, haha yeah i ripped it from that analogy :p
Johannes Schaub - litb
+2  A: 

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...

Matt Davison
C does not require explicit memory management; there are many GC libraries. Boehm is one.
jrockway
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
You could do pointers with unsafe code in C#
SQLMenace
+3  A: 

C++ is not a real superset of C either (see link text)

Lemmy
Technically yes... But practically it's pretty close. But C# isn't close to C in much besides syntax.
Mufasa
+2  A: 

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.

Ok, 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?
David B
I can compile C code within Obj-C or C++ using the appropriate compiler directives. That's my point
Robert Gould