As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm not a C++ guy, but I'm forced to think about this. Why is multiple inheritance possible in C++, when I'm not able to do it in C#? (I know the diamond problem, but that's not what I'm asking here). How does C++ distinguish the ambiguity of same method signatures inherited from multiple base classes? And why the same design can't be incorporated in C#?
This is a question of choice. Anders Hejlsberg, the C# language designer, chose to leave multiple inheritance out of the language. You may wonder why... My guess would be that (1) multiple inheritance is often not needed, (2) multiple inheritance is often used in the wrong way (like so many object-orientation constructs) and (3) it would make the language and/or the compiler and/or static checking unnecessarily complex.
The CLR itself does not prevent multiple inheritance; hence, it is available in C++.NET.
You can't in Java or C# because it's a design decision built into the language. Whether you agree or not, the language designers decided that the difficulties of multiple inheritance of implementation, as done in C++, wasn't worth the cost.
C++ already allowed multiple inheritance of implementation when I was writing it in 1995.
That choice was made by Java back in 1995. C# followed suit later on for the same reasons.
I'll point out that both Java and C# allow you to implement as many interfaces as you want. It's multiple inheritance of implementation that's the issue.
I'll leave the answer as to how C++ disambiguates multiple inheritance of implementation to others who have used the language more recently than me.
The ambiguity in the "diamond of death" is resolved using virtual inheritance. the wikipedia article is as good an explanation as any.
Virtual Inheritance
It is also potentially resolved by just designing your class hierarchy better. As a rule of thumb, multiple inheritance is ok as long as you're inheriting multiple interfaces (in c++, pure abstract classes).
From msdn.micosoft link
Multiple-inheritance is supported in C++. However, just about all the other modern object-oriented languages, including Java, have chosen not to allow multiple-inheritance. (Some advanced languages, such as Eiffel, have attempted to work out the kinks of multiple inheritance)
The biggest problem with multiple-inheritance is that it allows for ambiguity when the compiler needs to find the correct implementation of a virtual method.
So, in the interest of keeping things simple, the creators of Java and C# decided not to allow multiple inheritance. However, there is an alternative to multiple inheritance, known as interfaces
So keeping all these issues in mind designers doesn't allow multiple-inheritance in language, but still language support it in some other way like interfaces.
In addition of above answers & links you can also look to why doesn't c# support multiple inheritance
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Unfortunately, C# does not have anything like C++'s friend. Hence, for instance, in order to solve something like the classic matrix/vector multiplication example (where efficiency can be gained by befriending matrix and vector, such that each class has access to the private members of the other), I have to define the class members internal.
Now I know myself, and improper encapsulation will lead to messy code, sooner or later. Hence I would like to keep the internal universe as small as possible.
This will lead to very small assemblies.
Does that have any drawbacks, or doesn't that matter?
Answer to question in the title: how small assembly should be?
There is no particular requirements or recommendations on size of assemblies*.
*Insane number of assemblies (probably in thousands) in theory may slow down loading due to need to lookup information in more places.
When picking size consider:
proper encapsulation
ease of editing (large number of solutions vs. several large one both have drawbacks and benifits at build/edit/deploy time)
technical restrictions (like GAC deployment, partially trusted code, anyCPU/x86/x64 requirements)
If you don't like internal cause it's too open (to the rest of your api), make those classes that need to be open to eachother a separate microassembly containing just this small set of classes. You can always embed that assembly in your main api-assembly.
That should both keep your classes open to eachother, and not let anybody else in (unless they blatantly ignore your assembly-structure, and place more stuff in your helper-assembly than its name implies it should contain. But who would do such a thing?! :)
Unless I don't understand:
Unfortunately, C# does not have anything like C++'s friend.
Friend Assemblies (C# and Visual Basic) does exist. It may not be exactly what C++ has, but it sounds like it would work.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I've been using C/C++ and Python, but I now I see that a lot of new programming books use Java or C# as examples.
I don't think I'll use Java or C# for the time being, but I guess I have to study one of the languages (or both of them) in order to read/understand the books.
How similar Java and C#? If I learn Java, is learning C# almost free? Or vice versa?
If I have to choose only one of the two languages, which would be better? Which has wider coverage in terms of programming language?
You are asking several questions at once. Let me address them separately:
How similar Java and C#?
Both C# and Java drew from C/C++ (and Objective C, and others) to define their syntax. And both of them are compiled to an intermediate language.
This common origin makes the languages look similar in many levels, to the point that code in either language can be confused with the other by beginners; and also makes the runtime environment somewhat comparable. However, there are substantial differences in both design principles and how each language evolved that make working with each quite different; here are the most prominent ones:
On the syntax level, Java was influenced by Smalltalk, while C# tried to stay closer to C/C++ (eg: compare Java's extends and implements with C#'s : notation) and took a vague inspiration from VB on those concepts that weren't mappable to C/C++ (example: property syntax).
On the features level, C# 1 was definitely close to Java. Among the few differences they had, I'd highlight C#'s support for "unsafe" code (including pointers) and for delegates; and Java's controversial throws. This makes sense, since one of the goals of C# was to become an alternative to Java.
Many language features differ heavily on implementation details. For example, enums are very C'ish on C#, but are full objects in Java; or generics are implemented on the IL-level in C#, but in Java are dealt with via type erasure (neither is really close to C++'s templates besides syntax).
On the API level, they are worlds apart. C# relies on the .Net Framework, which was built on Microsoft's experience with the Visual Studio family of products (and thus is significantly Windows-oriented), while Java's Class Library was built, IIRC, from scratch, and heavily evolved over time (on these Swing days, does anyone remember AWT? I do).
Finally, it's worth mentioning that each of the languages has its own idioms, and its own community of supporters behind it.
If I learn Java, is learning C# almost
free? Or vice versa?
Neither. The key similarity is the basic syntax (semicolons, curly braces, array indexing, case-sensitiveness, etc), and you already have that from C/C++.
If I have to choose only one of the two languages, which would be better?
Short answer: flip a coin.
Long answer: it depends on your coding style and on what aspects of the language you value most. My best advise is to start by trying to learn both, until you feel that one of the languages pulls you more strongly than the other.
Alternatively, you can take a look at http://en.wikipedia.org/wiki/Comparison_of_Java_and_C_Sharp.
Which has wider coverage in terms of programming language?
If you mean language built-in features, I'd say C# wins for a narrow margin. Most of the features that C# has and Java lacks are syntax sugar (although they together make a significant difference on the learning curve and on the way the language is used). I value really high C#'s operator overload and extension methods. Also, LINQ is quite an interesting concept, but it is essentially a declarative syntax for loops.
Hope this helps.
The libraries are very different, and the approach to documentation is very different. I find the C# approach to easier.
To illustrate what I mean, in either language some object that you want to deal with may be implemented by inheriting from "object" to "generic object" to "specific object." In Java, if you want to find out about a property of the "generic object" you have to go to the doc for that, whereas in C# documentation, all the properties are listed for the "specific object."
At least that's the way it seemed to me back a few years ago when I did some Java.
If you want a brutally subjective answer.
I have just finished a degree in Computer Science in England, I chose a university that taught C# as opposed to Java.........reason being, so I could easily walk straight into a highly paid job as a software developer with a company that simply only wants people who know the "new" technologies.
These are companies such as Investment Banks, tech startups, IT Consulting firms.
So if you want to pick one to learn......
Think about the end goal.....if it's cash and modern knowledge...follow C#.
....and if it's anything else, follow C#
P.S. Java is terrible - I had to say it.
Not a 1:1 mapping since C# 1.0. C# has added lots of stuff (LINQ, closures,etc.) that have no analog in Java.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 13 years ago.
C# is owned by Microsoft and Java is owned by Sun/Oracle. What dangers does that really expose to the users of these languages? Has anyone felt their code was "owned"? Do projects like Mono help keep the "owners" honest?
Please do not make this a holy war of languages. I just want to know if it's rational to avoid such languages or if that's just paranoia. An interview with the inventor of C++ got me thinking, but I also want to balance his thoughts with the thoughts of the community as a whole.
As compared to what? Since you put it in these terms, the original C and C++ languages are "owned" by Bell Labs.
Java is not "Owned", it is open source. If you find a bug in it that you absolutely cannot deal with, you CAN fix it. (There are both open source and closed source implementations, however)
I don't know if you can get the source code to C#, but since Mono copied it there IS an open source for that as well.
I don't know if there is a second source for the .net libraries.
As for the actual "Dangers" (Which was your real question, after all), it would be that the company decides not to release updates any longer--if they do, will the language wither and die or will it take off on it's own? Java is in the process of transition from one of these states to another. Sorry, don't know about C#.
There is also the (Perceived) danger I mentioned earlier about--can you fix it if you hundred-million dollar company absolutely needs it fixed in order to continue.
This was a more significant problem twenty years ago, these days the fact is that if it's a good stable language, this isn't something you ever need to worry about.
No such danger for C# language. It is an ISO standard. Formally it is owned by a committee. But Java is a trademark
Getting up in the morning is risky, but that doesn't keep the world under the covers.
I feel like this is one of those acceptable risks. In Java's case, companies have used it for the last 15 years or so to their benefit.
What's the alternative? Developing and maintaining your own language so you own it? That's what SAP did. It seems to have worked out for them, but it'd be interesting to calculate the cost they've incurred.
Bjarne Stroustrup is a brilliant man, but let's not forget that he has biases. He isn't happy that Java eclipsed C++ as the primary object-oriented language when it came out. He's attributed it to Sun's marketing, not conceding that it might have improved on C++.
It's a good practice to try and spot biases on the part of any speaker to make sure you're not swallowing someone's view whole. This is one of those cases.
If there are not two independent implementations, language is "Owned" and you are at the mercy of the vendor should he raise prices or can the product.
I don't like that.
EDIT: As often as not, you can count legally forkable codebases as two (the second is yourself).
Aren't all languages owned by a person/company/standards body. The only way I can think of where it isn't really owned by anybody is if the person who made it is anonymous and also public domain
hmm, well Xbox only supports C# for indie games, and no other platform supports it.
obviously the danger is that if you want to do multi platform code, you want the language supported by the most platforms, the more "owned" language is probably going to be supported by less platforms.
the only issue I have is support for the language, and how hard it is to convert from one to the other, for instance I would say c++ to c# is easier than the other way because of memory management.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I've been a "Microsoft developer" ever since I started programming... I started out by learning QBasic then ASP & Visual Basic and finally I moved on to learn VB.NET, ASP.NET and C# which is now my primary language. Still I've always wanted to learn something like Java or C++ to see how what the "other side" is like, and to learn the pros and cons of each platform. The only problem is that I never found the time or opportunity to pick up another language.
My basic understanding is that Java and C# have a lot of similarities as well as a lot of differences. Coming from C# I'm mostly interested in what C# could learn from Java, or put another way, what I'm missing out on.
Nothing, C# based itself on Java :)
prepares to be ridiculously downvoted
Should support Java like Enums, In C# they are just name for numbers !!
I think C# would be better off if they enforced the one public class per file and forced you to put your code files in a directory structure which matches the namespace (i.e. "package"). This is one thing I really like about Java that frustrates me in C#, especially when people start moving .cs files around, and putting multiple public classes in one file.
Personally I would have preferred to use the Java style for properties. I don't like the fact that properties in C# looks exactly like fields and yet have entirely different semantics. By making it clear that properties are really just methods there is no illusion and less risk of mistakes.
Anonymous interface implementation - Java does it, C# does not (yet).
For example:
// C#
interface IRunnable
{
void Run();
}
var runnable = new IRunnable()
{
public void Run()
{
Console.WriteLine("Running...");
// Do your running
}
};
runnable.Run();
checked exceptions
package visibility for submodules. AFAIK you can only have one module per assembly. I mean a group of tightly coupled classes which can see each other but should be hidden from the outside world.
C# obviously learned a great deal from Java, just like Java did from C++. They've traded back and forth in both language features (e.g., Java taking annotations from C#) and ancillary projects (NHibernate, NAnt, Spring.NET, POJO/POCO based development, etc. all have their origins in Java).
But I'm afraid that Java's not advancing anymore now that Sun lost market and people and has been sold to Oracle. I don't see anything coming out of Java or vendors that looks terribly new right now. Java 7 has been a long time coming, and it's catching up with closures and other things.
I think whatever energy is left in language development has passed to dynamic and functional languages.
The only development that I'm aware of that could change that is Rikard Oberg's Qi4J. He's so out there. It's just going to be a long time before it becomes mainstream, if ever.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 14 years ago.
What do you see as benefits for one over the other? As far as I know, it's just preference. I have way more experience with C# than I do with Visual Basic and was wondering if I should dabble in Visual Basic for certain types of projects?
VB4 was my first language, with VB6 being the last time I touched it. I have moved to c# and wouldnt consider going back. VB just feels too fat and fluffy for me (preference).
All the .NET languages compile to IL though...
Note: c# is "closer" to java...
I personally prefer C#, I love the syntax and I feel really comfortable with the language.
Some say that C# programmers are paid better than VB programmers, but I think you should try both languages and see on which you fell better.
Top 10 reasons C# is better than VB.NET
Top 10 reasons VB.NET is better than C#
If you plan on doing any SSIS you will need to know VB. This one of few areas that I am aware of in which it truly matters which language you choose as SSIS only supports VB for any "integrated" code you write for it.
You are correct in saying that it is a preference as all .NET languages are compiled to IL. So choose the one you are most comfortable with and don't worry too much about it.
Coming from a curly-braces background I find c# to be a lot easier to read and write. I find VB.Net to be too verbose and some of the syntax (I'm looking at you, arrays) makes my eyes water.
I also get frustrated by the background compilation in VB.Net, especially on large projects where it can make the IDE unresponsive.
The only advantage VB.Net has over C# in my opinion is optional parameters. These make certain interop tasks a lot easier but I think c# is due to get them in 4.0.
VB.NET
In my opinion, C# was created only for marketing reasons to bring Java developers to .NET.
There are many more developer jobs in the job marketplace for C# over VB. Visual Basic got a bad wrap from the get go because it was an interpreted language. Back in the early days of computers, interpreted was bad and slow.
In the beginning, Microsoft built VB mostly for consultants to be able to quickly and effectively write internal software.
I cringe when I see C#, but these days, I write in JavaScript more than any other language and I love it.
VB's language keywords makes more sense to me over C#'s such as Imports vs Using. With declarations, I do not like having object type coming before the variable's name. And it seems that C# has many hidden rules with parenthesis and what not that a developer must know just to even read C#. Whereas VB is straight to the point and flows very nicely without crazy syntaxes.