How is C# inspired by C++ more than by Java? [closed]
Asked Answered
I

18

23

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?

Innocuous answered 10/12, 2009 at 21:55 Comment(7)
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#).Jeep
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.Uchida
@Jeep about complexity Javs vs. C#: specs for Java are twice the size of specs for C#.Strophic
C# is actually mostly inspired by Delphi.Purism
@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..Annular
another point java and c# are arguably closer at the standard library level as well, mainly due to c++ having far smaller std libraryTonetic
@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.Phyllisphylloclade
P
40

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

Phyllisphylloclade answered 10/12, 2009 at 22:20 Comment(7)
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....Jeep
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.Phyllisphylloclade
Well then, you must admit that it's just like Java, right? I mean, it's certainly not like anything in C++.Phyllisphylloclade
The fact that not every method is virtual by default does show some influence by C++Ferrosilicon
@Colin Newell: excellent point.Phyllisphylloclade
@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.Cowling
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.Talky
S
56

Its name starts with the letter C.

Spud answered 10/12, 2009 at 22:43 Comment(3)
In direct answer to "how is C# closer to C++ than to Java?" this is an artful response!Farhi
LOL... you surely got the most number of upvotes on this google mine! :DLimner
I thought it was because they got creative with naming after saying c plus plus ... plus plus C++ ++ -> C#. You can better see this on a piece of paperPasahow
P
40

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

Phyllisphylloclade answered 10/12, 2009 at 22:20 Comment(7)
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....Jeep
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.Phyllisphylloclade
Well then, you must admit that it's just like Java, right? I mean, it's certainly not like anything in C++.Phyllisphylloclade
The fact that not every method is virtual by default does show some influence by C++Ferrosilicon
@Colin Newell: excellent point.Phyllisphylloclade
@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.Cowling
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.Talky
F
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.

Finicking answered 10/12, 2009 at 22:0 Comment(8)
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.Druse
+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.Jeep
@steven: this is the price you pay for being a mainstream language.Strophic
@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 badStrophic
@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!Finicking
@Strophic Pure-OOP != Good Code.Finicking
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.Peele
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 createdCowling
U
13

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.

Uchida answered 10/12, 2009 at 22:21 Comment(3)
+1 - Isn't there a saying that history is written by the winners?Spear
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.Surd
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.Surd
D
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.

Dyer answered 10/12, 2009 at 22:12 Comment(0)
S
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

Surd answered 10/12, 2009 at 23:0 Comment(4)
+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#.Phyllisphylloclade
Agreed. Sun didn't do themselves any favors. They should have been focused on fixing Swing. :)Surd
@Phyllisphylloclade Agreed. And here they are 10 years later finally adding closures to Java.Fontenot
@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.Surd
R
5

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

Remind answered 10/12, 2009 at 21:58 Comment(8)
Yes, but much more restricted than C++. You can only create pointers to value types.Varicolored
@Max: Any pointer manipulation is closer to C++ than Java. :)Phia
Yes, and any single inheritance is closer to Java than to C++. ;)Varicolored
Yes, but having to specify that a function is virtual is close to C++ than to Java ;)Jeep
Yes, and that eventually boils down to the fact that C# was inspired by both languages and added it's own stuff to.Varicolored
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>Jeep
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.Misdeal
@Mark: defining, no. Differentiating, yes. Rarely used is quite a bit different than COMPLETELY IMPOSSIBLE.Remind
C
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.

Ceric answered 10/12, 2009 at 22:7 Comment(0)
G
4

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

Gunning answered 10/12, 2009 at 21:59 Comment(4)
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.Varicolored
How would knowing the answer prove that you a viable candidate?Absa
I guess the interviewer assumed C# was modeled on Delphi because C# lead architect, Anders Hejlsberg, was also the chief architect of Delphi (cf en.wikipedia.org/wiki/Anders_Hejlsberg )Sobel
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. :>(Gunning
V
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.

Varicolored answered 10/12, 2009 at 21:59 Comment(0)
C
3

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.

Cowling answered 11/12, 2009 at 12:25 Comment(0)
J
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
Jeep answered 10/12, 2009 at 22:6 Comment(6)
All non-static java methods are virtual. All objects are passed by reference. Your syntax examples are nothing more than that, syntax.Foppish
Language is syntax. Everything else is runtime structure.Phyllisphylloclade
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.Phyllisphylloclade
@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.Jeep
@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.Sept
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.Phyllisphylloclade
P
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.

Potts answered 14/12, 2017 at 22:13 Comment(0)
F
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.

Foppish answered 10/12, 2009 at 22:40 Comment(0)
W
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++.

Wurster answered 10/12, 2009 at 22:57 Comment(0)
H
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.

Holierthanthou answered 10/12, 2009 at 23:12 Comment(0)
O
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#
Oasis answered 10/12, 2009 at 22:37 Comment(7)
Smalltalk is a bit of a slut, eh?Foppish
I'd say: C + Smalltalk => Objective COsteoporosis
C + Smalltalk --> shredding machine =(vomits)> C++Farhi
@Software Monkey: I'd upvote your comment more than once if I could.Sept
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++.Cowling
@jalf: And they are? Maybe Smalltalk is a grandmother of C++ :-)Oasis
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.Demello
A
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++.

Anta answered 10/12, 2009 at 22:46 Comment(2)
Which elements? I can't think of much. Every powerful feature of C++ that I know of and use, is gone in C#.Cowling
I had figured that prefacing my statement with "managed version" would allow me to make a case in how C# derives from C++...Anta

© 2022 - 2024 — McMap. All rights reserved.