22

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?

Grace Note
  • 3,205
  • 4
  • 35
  • 55
Mathias
  • 15,191
  • 9
  • 60
  • 92
  • 1
    Part of the reason that you can understand Java better than C++ is because java is a fairly simple language compared to C++ (and compared to C#). – tster Dec 10 '09 at 21:59
  • 1
    On the wikipedia page you reference they list some distinguishing features of CC#. Almost every one is shared with Java. I didn't see one thing that it shared with C++ that Java didn't have as well. – Bill K Dec 10 '09 at 22:31
  • @tster about complexity Javs vs. C#: specs for Java are twice the size of specs for C#. – mfeingold Dec 10 '09 at 22:43
  • 5
    C# is actually mostly inspired by Delphi. –  Dec 10 '09 at 23:04
  • @wim might be true, but we are missing the point here, specification for Java ARE twice bigger, yes - but the Java front end to user (programmer) - reserved words, API.. - is what we should be talking about when talking about simplicity, not the specification about VM, or rules how should be compiler built.. – ante.sabo Dec 11 '09 at 08:10
  • another point java and c# are arguably closer at the standard library level as well, mainly due to c++ having far smaller std library – jk. Dec 11 '09 at 12:35
  • @Niel Butterworth: I think it's safe to say that the API and some of the features are inspired by Delphi, yes, but the syntax and runtime? No way. – Randolpho Dec 14 '09 at 15:13

18 Answers18

55

Its name starts with the letter C.

Crashworks
  • 40,496
  • 12
  • 101
  • 170
37

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
  • 55,384
  • 17
  • 145
  • 179
  • 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 Dec 10 '09 at 22:31
  • 10
    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 Dec 10 '09 at 22:36
  • 1
    Well then, you must admit that it's just like Java, right? I mean, it's certainly not like anything in C++. – Randolpho Dec 10 '09 at 23:12
  • 5
    The fact that not every method is virtual by default does show some influence by C++ – Colin Newell May 18 '10 at 14:53
  • @Colin Newell: excellent point. – Randolpho May 18 '10 at 15:08
  • 3
    @Colin: or some influence from *nearly every other language in existence*. The absence of a feature doesn't really tell us much. It's more interesting to look at the features *present*. – jalf Dec 26 '10 at 09:25
  • Even though they both compile into bytecode languages and both have garbage collection, I don't think they're so similar. C#'s methodology is like "abstract C++" providing the niceties of higher-level languages while acting like managed C++ under the hood. Java acts nothing like managed C++ under the hood; reference types and style of memory consumption are part of that difference. – That Realty Programmer Guy Jan 03 '14 at 07:54
18

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.

Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
  • 3
    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 Dec 10 '09 at 22:09
  • 12
    +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 Dec 10 '09 at 22:12
  • @steven: this is the price you pay for being a mainstream language. – mfeingold Dec 10 '09 at 22:46
  • @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 Dec 10 '09 at 22:48
  • @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! – Khaled Alshaya Dec 11 '09 at 00:21
  • 6
    @mfeingold Pure-OOP != Good Code. – Khaled Alshaya Dec 11 '09 at 00:23
  • 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 Dec 11 '09 at 00:28
  • 2
    Worth noting that C# wasn't *created* as a multi-paradigm language. It was created for OOP and just OOP. Everything else was added onto it later. So while it is true today that C#, like C++, is a multi-paradigm language, I don't think it's true to say that C++ being multi-paradigm was an inspiration when C# was created – jalf Dec 26 '10 at 09:26
12

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 and called it "J++".

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
  • 62,186
  • 18
  • 105
  • 157
  • +1 - Isn't there a saying that history is written by the winners? – Stephen C Dec 11 '09 at 07:10
  • 1
    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 Dec 11 '09 at 20:50
  • 5
    Also, J++ was 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 Dec 12 '09 at 14:59
8

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
  • 6,811
  • 4
  • 37
  • 53
7

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
  • 5,060
  • 3
  • 24
  • 26
  • 2
    +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 Dec 11 '09 at 04:10
  • 1
    Agreed. Sun didn't do themselves any favors. They should have been focused on fixing Swing. :) – Dave Sims Dec 12 '09 at 02:41
  • 1
    @Randolpho Agreed. And here they are 10 years later *finally* adding closures to Java. – Mud Dec 19 '10 at 11:08
  • 1
    @Mud: Wait for it...wait for it...oops, kicked closures down to Java 8, not 7. You can't rush into these things I guess. – Dave Sims Jan 24 '11 at 21:00
5

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

Stu
  • 15,675
  • 4
  • 43
  • 74
5

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
4

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
  • 11,253
  • 2
  • 33
  • 40
3

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
  • 21,522
  • 8
  • 49
  • 87
  • 11
    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 Dec 10 '09 at 22:00
  • 6
    How would knowing the answer prove that you a viable candidate? – Giovanni Galbo Dec 10 '09 at 22:16
  • 4
    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 ) – Aurélien Gâteau Dec 10 '09 at 22:17
  • 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 Dec 10 '09 at 22:37
2

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
  • 17,883
  • 5
  • 53
  • 72
  • 4
    All non-static java methods are virtual. All objects are passed by reference. Your syntax examples are nothing more than that, syntax. – Jherico Dec 10 '09 at 22:36
  • 4
    Language *is* syntax. Everything else is runtime structure. – Randolpho Dec 10 '09 at 22:38
  • 1
    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 Dec 10 '09 at 22:39
  • 3
    @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 Dec 10 '09 at 22:53
  • 1
    @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 Dec 11 '09 at 05:03
  • 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 Dec 11 '09 at 15:01
2

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
  • 243,077
  • 51
  • 345
  • 550
2

FWIW: From a historical perspective prior C# coming on the scene MSFT was promoting Visual J++ which was an implementation of Java with some enhancements to enable utilizing windows specific features.

Sun Microsystems sued as their goal with Java was a write once run anywhere language and Visual J++ would result in applications that would only on Windows, but not on Sun's Unix implementation.

Sun prevailed in court and MSFT withdrew Visual J++ from the market and very shortly later announced Visual C# which was trivially different syntactically and functionally identical to the Visual J++ language just withdrawn.

1

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,584
  • 8
  • 61
  • 87
1

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
  • 17,306
  • 6
  • 61
  • 82
1

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
  • 996
  • 1
  • 8
  • 17
0

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
  • 257,169
  • 86
  • 333
  • 562
0

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

Nick Vaccaro
  • 5,428
  • 6
  • 38
  • 60
  • Which elements? I can't think of much. Every powerful feature of C++ that I know of and use, is gone in C#. – jalf Dec 11 '09 at 12:19
  • I had figured that prefacing my statement with "managed version" would allow me to make a case in how C# derives from C++... – Nick Vaccaro Dec 11 '09 at 14:46