views:

924

answers:

17

When looking at the history of C#, I found out that C# was seen as an update to C and/or C++. This came a bit as a surprise to me, as on the surface, I see much more common ideas between C# and Java (Garbage collection comes to mind). I don't write code in Java, but I have usually no problem following Java code, and routinely read books about patterns in Java that I can readily transpose in C#, and can honestly not say the same about C++.
So my question is, how is C# closer to C++ than to Java? Is this simply a refusal to acknowledge Java, or am I missing or misunderstanding something?

+4  A: 

For starters, it allows pointer manipulation (in unsafe blocks).

Stu
Yes, but much more restricted than C++. You can only create pointers to value types.
Maximilian Mayerl
@Max: *Any* pointer manipulation is closer to C++ than Java. :)
280Z28
Yes, and any single inheritance is closer to Java than to C++. ;)
Maximilian Mayerl
Yes, but having to specify that a function is virtual is close to C++ than to Java ;)
tster
Yes, and that eventually boils down to the fact that C# was inspired by *both* languages and added it's own stuff to.
Maximilian Mayerl
Yes, and that really means that C# is more like C++ instead of dogmatically forcing programmers to write code the way James Gosling thinks they should write it. </flamebait>
tster
I'm not sure why people are upvoting this. While it's true that C# has pointers, you rarely have to use them. I.e. Pointers = not a defining language feature.
Mark Simpson
@Mark: defining, no. Differentiating, yes. Rarely used is quite a bit different than COMPLETELY IMPOSSIBLE.
Stu
+1  A: 

I had an interview once ask me what language C# was modeled on and I quickly said a better C++ or Java (C--). The interviewer said "Wrong!", it's Delphi. I don't know Delphi, so...

kenny
What stupid interview question is that? C# was inspired by *many* languages, it's absolute nonesense to say it's was modelled after *one* language.
Maximilian Mayerl
How would knowing the answer prove that you a viable candidate?
Giovanni Galbo
I guess the interviewer assumed C# was modeled on Delphi because C# lead architect, Anders Hejlsberg, was also the chief architect of Delphi (cf http://en.wikipedia.org/wiki/Anders_Hejlsberg )
agateau
Yeah, my feeling was it was a silly question as well. I especially liked his "Wrong!", which I laughed when it was said. He did it again on another question as well, but I've forgotten that one. :>(
kenny
+4  A: 

Well, I think you will start a little argument over this.

I think Java is a kind of update/evolution of C++ too, so this would of course explain the similarity to some extent. The truth is of course that C# was inspired by both Java and C++ and took concepts out of both languages. And of course, there is much in the field of functional programming and dynamic programming getting adapted to C#.

So I think it is wrong to say that C# is "closer" to Java than to C++ or the other way round. It's in some way close to both.

Maximilian Mayerl
+13  A: 

I think in the sense that C# is more of a multi-paradigm language rather than forcing a single paradigm(OOP) like in JAVA. It is a good thing actually, because it gives more freedom for its programmers.

AraK
I disagree with you on this, this produces many varying types of C# developers, which can make a codebase a bit mangled and coding standards harder to enforce.
monksy
+1 to this. And @steven, I think allowing freedom is good in a language. The functional stuff in C# allows me to write much more expressive, compact, and understandable code than I could in Java.
tster
@steven: this is the price you pay for being a mainstream language.
mfeingold
@tster: I hope they(MS) will not push it too far. Adding too much functional stuff to C# will make it more difficult to do pure OOP. If you want functional - go F#. So far it is not too bad
mfeingold
@steven Giving freedom is giving various types of tools to do the job. This would create idioms in the language instead of trying to bend the neck of the language to do things that aren't supported in it. e.g. compare how you write lambda in C# VS Java. I know :) in Java you create an anonymous class! why do I have to write all this "sh*t" to get a one-liner lambda!
AraK
@mfeingold Pure-OOP != Good Code.
AraK
Fair comments about java, however java is only one language that can be compiled to java byte code. If you want to do functional stuff you have scheme or if you want lots of code that is easy to type you have groovy.
vickirk
+2  A: 

In the OO and inheritance aspects, C# is more similar to C++. For example:

class MyClass : MyInterface

instead of

class MyClass implements MyInterface

and

public MyClass() : base()

instead of

public MyClass() { super(); }

Also C# uses the idea of virtual functions to allow overloading. In java, inheriting and overloading is more of an open affair that must be locked down. In C++ and C#, it is by default more locked down, and must be opened up.

Other similarities include:

  • Pass by reference
  • operator overloading
  • function pointers/delegates
tster
All non-static java methods are virtual. All objects are passed by reference. Your syntax examples are nothing more than that, syntax.
Jherico
Language *is* syntax. Everything else is runtime structure.
Randolpho
Also, Pass by reference is referring to the ability to pass *value types* (i.e. primitives in Java) by reference. Something Java lacks, and something I totally neglected to mention in my answer.
Randolpho
@Jherico, Java is **PASS BY VALUE ONLY**. The value that is passed when you pass an object is an object reference. Pass by reference like you have in C# (and supported in C++) would imply that you could change the identity of the object within the calling code from within the called function. Also, syntax and semantics define the language, I don't know what other types of examples to give.
tster
@Randolpho: Well, I'd disagree with that. I'd say that a language is mostly defined by the programming model it exposes. Changing the brace style of a language doesn't really change its nature, compared to differences between, say, C# and LISP.
kyoryu
Really? Compare VB.NET and C#. Both compile down to the same runtime. Yet there are things C# can do that VB.NET cannot. And vice versa.
Randolpho
+4  A: 

Personally I would say it is closer to Java than C++, but a few extras for the melting pot:

  • Delegates (roughly function pointers)
  • User-defined value-types

Generics are different for all 3, but Java's type-erasure generics are probably (although it is an odd comparison) closer to C++ templates than C# runtime-based generics.

Marc Gravell
+3  A: 

Syntax inspired by C++, VM inspired by Java, and libraries (in .net 1.1) were almost a 1-1 correlation with the Delphi libraries.

John Kraft
+22  A: 

IMO, the idea that C# is inspired more from C++ than Java is marketing only; an attempt to bring die-hard C++ programmers into the managed world in a way that Java was never able to do. C# is derived from Java primarily; anyone who looks at the history, particularly the Java VM wars of the mid 90s between Sun and Microsoft, can see that Java is the primary parent.

The syntax of C# is closer to C++ in only certain areas: pointer manipulation (which Java doesn't have), derivation declaration (i.e. public class Foo : Bar, IBaz rather than public class Foo extends Bar implements IBaz), and operator overloading.

Everything else is either just like Java (static main declared in a class declaration, no header files, single inheritance, many others), just like both Java and C++ (basic syntax), or uniquely C# (properties, delegates, many many others).

Randolpho
saying that static main inside a class declaration is common is stretching it. Code can only go in classes, and how else are you going to start a program unless it is a static function? Couple that with the fact that "main" as the start has been around for a while....
tster
In C++ the main function is a global function, not a static method, and you're wrong; all sorts of code in C++ can go in stuff other than classes. C++ is just C with syntactic sugar on top, remember? Ok, that last sentence is entirely inaccurate, but it gets the point across.
Randolpho
I was talking about C#.
tster
Well then, you must admit that it's just like Java, right? I mean, it's certainly not like anything in C++.
Randolpho
The fact that not every method is virtual by default does show some influence by C++
Colin Newell
@Colin Newell: excellent point.
Randolpho
+10  A: 

If you remember the history, Microsoft actually tried to use their "Embrace and Extend" methodology on Java where they add to it so that it will only work with windows.

Sun sued them so they came out with J++ which was very close to Java as a stop-gap which gave them time to develop a language of their own (C#).

At that point, it was a mix of the best concepts from all the languages available at the time--it doesn't really make sense to say that it was more based on one than the other, it's just a conglomeration of selected features.

But if you like this idea of starting from scratch and working with all of the best features of languages to date, be sure to check into Scala--it's gone way beyond what Java or C# can do, even with a major feature revolution.

Bill K
+1 - Isn't there a saying that history is written by the winners?
Stephen C
Actually J++ wasn't a stop-gap, it WAS the 'embrace and extend' that provoked the suit to begin with. J# was the stop-gap.
Dave Sims
Also, J++ <i>was</i> Java 1.1, with the suit-provoking exceptions of JNI support and the directives I mentioned below. J++ ran on the MSVM, which supported up to Java 1.1, and had to stop there because of a court injunction. Sweet Lord I know too much about this. Yes, I was stuck in J++ hell for a number of years, although at first it was actually the best 3GL IDE option out there for Windows. You got the best of Delphi and Java mashed together, and none of the arbitrary pain of VB 6.
Dave Sims
+1, for mentioning Scala! :)
missingfaktor
A: 

All three are just close relatives.
Whose mother was smalltalk.

Who the father of each language is another question?

Ada   + Smalltalk  =>  Java
C     + Smalltalk  =>  C++
?     + Smalltalk  =>  C#
Martin York
Smalltalk is a bit of a slut, eh?
Jherico
I'd say: C + Smalltalk => Objective C
Maha
C + Smalltalk --> shredding machine =(vomits)> C++
Software Monkey
@Software Monkey: I'd upvote your comment more than once if I could.
kyoryu
I think it's a bit of a stretch to say that Smalltalk was the "mother" of C++. I can think of several languages that were bigger inspirations for C++.
jalf
@jalf: And they are? Maybe Smalltalk is a grandmother of C++ :-)
Martin York
Stroustrup talks about adding classes to C in his "Design and Evolution" book. He was explicitly modelling them after Simula, not Smalltalk, based on his experience with Simula 67 in the late 1970s. The parents of C++ are Simula (the first object-oriented language, by some useful definitions) and C.
David Thornley
+1  A: 

I'll probably take flak for this, but yes, C# is by and large Microsoft's answer to Java. Its a language they can extend any which way they choose (unlike Java where they were censured for extending it in non-approved ways). It has the critical features of Java: memory management and a large system library. It is C++-like or C-like in as much as it need to be to lure C++ and C developers who aren't already fans of Java.

Jherico
+28  A: 

Its name starts with the letter C.

Crashworks
In direct answer to "how is C# closer to C++ than to Java?" this is an artful response!
Software Monkey
+1 bazillions. You win!
Randolpho
A: 

I see C# as a managed version of C++, rather than a rewrite of Java. In creating such a language, it naturally took the form of Java (syntactically) but retains the elements of the incredibly powerful C++.

Norla
Which elements? I can't think of much. Every powerful feature of C++ that I know of and use, is gone in C#.
jalf
I had figured that prefacing my statement with "managed version" would allow me to make a case in how C# derives from C++...
Norla
+1  A: 

One example is the comparison operator == on strings. C# takes the C++ approach and does a lexical compare on the string. Java compares string references.

Here's a good MSDN article that takes you through a comparison between C# and Java and C# and C++.

indiv
+6  A: 

I think the Java -> C# connection is much stronger than C++ -> C#, mainly because, as has been pointed out, C# was the result of the Sun suit over the MSVM's alleged violation of the Java spec. In particular it was J++'s use of additional keywords/functionality like 'delegate', which became one of the main distinguishing features of C#, that was one of Sun's main complaints.

Other functions like the @dll and @com directive (which prefigured attributes in both Java and C#) were also part of the complaint. Note the similarity between C#'s COM PIA directives and the @com directives in J++. Compare J++'s JDirect with native interop in C#. (Another reason for the suit was the fact that MS disabled Java's JNI altogether in favor of the Windows-specific JDirect.)

Finally, Anders Hejlsberg is the man responsible for both J++ and C#, so the connection between Java/J++ and C# is pretty dang solid. No doubt Hejlsberg had many aspects of C++ mind (in particular the method pointer/delegate feature, which he floated first in J++), but it's safe to say Java had to have been forefront.

In many ways you can look at Microsoft's altered version of Java, J++ as C# 0.1

Dave Sims
+1. Highly accurate. Somebody remembers the whole bru-haha in the mid 90s. :) At the time, I was kinda mad a Sun because I liked the J++ changes; had they *adopted* the changes that MS suggested (MS did ask Sun nicely at one point, IIRC) rather than fought them, C# would never have existed. So at the time, I was mad at Sun. Now I'm very happy. I loves me some C#.
Randolpho
Agreed. Sun didn't do themselves any favors. They should have been focused on fixing Swing. :)
Dave Sims
+1  A: 

For what it is worth C#'s Forms are very closely related to Java's Swing. C++'s Gui libraries are in most cases quite different.

dennisjtaylor
+1  A: 

It's marketing. Microsoft certainly didn't want to admit that "Hey, we're making Java 1.1!" First, it would be the same as admitting that Java is actually worth copying, which is a bad move if you're trying to beat Java.

And second, it would scare off all the native C++ developers who got scared off by Java's clumsiness and early performance problems.

So they say that they're building on C++ and everyone's happier.

In reality, of course, nothing could be further from the truth.

If C# and Java have a common ancestor, it is not C++, but "C with classes"; specifically, the very earliest versions of C++, long before it was standardized, and before most of what makes it useful today was added.

It was little more than an attempt at bolting some OOP functionality onto C.

C++, since then, has gone in a completely different direction, letting go of the OOP obsession, and exploring a much more functional style, using a compile-time form of duck-typing to create generic programming. Some incredibly powerful and elegant libraries have been added to the language. None of that is at all represented in Java or C#.

C# is definitely inspired by Java more than anything. And where it is inspired by C++, it is inspired by this early "C with classes" variant, rather than anything resembling modern C++.

But C++ was, and is, considered a "cool" language by many. Microsoft wanted to tap into that "cool" and bring it to .NET.

Personally, I'd consider C++, C# and Java to be siblings. They're all derived from the same "C with classes" protolanguage. C# and Java took a less direct route from there than C++ did, but it's still where most of their inspiration came from. That's where they inherited the weird notion of OOP that has virtually nothing to do with what Alan Kay/Smalltalk proposed, and it's where they inherited the clumsy C-like syntax.

It's a bit like saying that we humans are evolved from apes. We're not. We just have a common ancestor, and that common ancestor was somewhat ape-like. C# isn't derived from C++, they just have a common ancestor that was slightly C++-like.

jalf