开发者

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

开发者 https://www.devze.com 2022-12-13 15:10 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. 开发者_运维技巧 Want to improve this question? Update the question so it can be answered with facts and citati
Closed. This question is opinion-based. It is not currently accepting answers. 开发者_运维技巧

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 4 years ago.

Improve this question

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?


Its name starts with the letter C.


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


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.


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.


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


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


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


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.


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.


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


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


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.


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.


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.


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


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.


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#


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号