C# seems to be popular these days. I heard that syntactically it is almost the same as Java. Java and C++ have existed for a longer time. For what reasons should I choose C# over Java and C++?
The question should be "Which language is better suited for modern, typical application development?".
Edit: I addressed some of the comments below. A small remark: consider that when you have a lot of things natively, as idioms, it's a big difference than implementing or downloading and using them yourself every time. Almost everything can be implemented in any of these languages. The question is - what the languages natively provide you with.
So off the top of my head (some arguments apply to both languages)...
C# is better than C++ in that:
- It has native garbage-collection.
- It allows you to treat class-methods' signatures as free functions (i.e. ignoring the statically typed
thispointer argument), and hence create more dynamic and flexible relationships between classes. edit if you don't know what this means, then try assigning a member method returning void and accepting void to a
void (*ptr)()variable. C# delegates carry the
thispointer with them, but the user doesn't always have to care about that. They can just assign a
void()method of any class to any other
- It has a huge standard library with so much useful stuff that's well-implemented and easy to use.
- It allows for both managed and native code blocks.
- Assembly versioning easily remedy DLL hell problems.
- You can set classes, methods and fields to be assembly-internal (which means they are accessible from anywhere within the DLL they're declared in, but not from other assemblies).
C# is better than Java in that:
- Instead of a lot of noise (EJB, private static class implementations, etc) you get elegant and friendly native constructs such as Properties and Events.
- You have real generics (not the bad casting joke that Java calls generics), and you can perform reflection on them.
- It supports native resource-management idioms (the
usingstatement). Java 7 is also going to support this, but C# has had it for a way longer time.
- It doesn't have checked exceptions :) (debatable whether this is good or bad)
- It's deeply integrated with Windows, if that's what you want.
- It has Lambdas and LINQ, therefore supporting a small amount of functional programming.
- It allows for both generic covariance and contravariance explicitly.
- It has dynamic variables, if you want them.
- Better enumeration support, with the
- It allows you to define new value (or non-reference) types.
Edit - Addressing comments
- I didn't say C++ doesn't support native RAII. I said Java doesn't have it (you have to explicitly do a try/finally). C++ has auto pointers which are great for RAII, and (if you know what you're doing) can also substitute garbage-collection.
- I didn't say anything about emulating free functions. But for example if you need to access a field by a
thispointer, and bind the method that does it to a generic function pointer (i.e. not in the same class), then there's simply no native way to do it. In C#, you get the for free. You don't even have to know how it works.
- By "treating member methods as free functions" I meant that you can't, for example, natively bind a member method to a free function signature, because the member method "secretly" needs the
usingstatement, obviously along with IDisposable wrappers, is a great example of RAII. See this link. Consider that you don't need RAII as much in C# as you do in C++, because you have the GC. For the specific times you do need it, you can explicitly use the
usingstatement. Another little reminder: freeing memory is an expensive procedure. GC have their performance advantage in a lot of cases (especially when you have lots of memory). Memory won't get leaked, and you won't be spending a lot of time on deallocating. What's more, allocation is faster as well, since you don't allocate memory every time, only once in a while. Calling
newis simply incrementing a last-object-pointer.
- "C# is worse in that it has garbage collection". This is indeed subjective, but as I stated at the top, for most modern, typical application development, garbage collection is one hell of an advantage.
In C++, your choices are either to manually manage your memory using
delete, which empirically always leads to errors here and there, or (with C++11) you can use auto pointers natively, but keep in mind that they add lots and lots of noise to the code. So GC still has an edge there.
- "Generics are way weaker than templates" - I just don't know where you got that from. Templates might have their advantages, but in my experience constraints, generic parameter type-checking, contravariance and covariance are much stronger and elegant tools. The strength in templates is that they let you play with the language a bit, which might be cool, but also causes lots of headaches when you want to debug something. So all in all, templates have their nice features, but I find generics more practical and clean.
.NET Framework and Windows clients
Windows is the dominating Operating System on client computers. The best GUI frameworks for Windows applications is Winforms and WPF together with .NET Framework. The best programming language to work with the .NET Framework and it's APIs is C#. Java is not an alternative for this. And C++ is an older language without automatic memory management. C# is similar to C++ but has automatic memory management and you don't have to work with pointers, which make you more productive. C++ can still be the best option for some cases, but not for form-intensive database applications that is common in business.
IIS and Windows Server
If you are used to work in the Windows environment and with C#, you will need the least investment to learn IIS for server programming and Windows Server for basic administration.
Active Directory and Windows Server
If you are developing software that is going to be deployed in company networks, it's likely that they use an Windows centered environment using a Windows Server with Active Directory. In such an environment it's easist to integrate and deploy an solution made in C# and .NET Framework.
Personally, I'm a Java developer, not a C# developer, but I work with the web. I would switch to C# if I were developing network applications for Windows network. But I prefer Java for Linux based web servers. I would choose C++ for embedded systems were I don't won't many dependecies.
Yes, C# is a better language with more modern features than C++ and Java, but that is not the most important thing for choosing C#.
The environment for your software is most important for choosing C#. If you work in an environment with Windows clients, Windows servers, Active Directory, IIS and maybe SQL Server then C# is the far best language with the .NET Framework.
If you work in a Unix environment with e.g. web services, Java would be my choice. And if you work with embedded systems or have to integrate with hardware devices C++ would be a good choice.
C# and Java
C# is a very good language if:
- You want to do general purpose object oriented development. It's a classic, statically typed OOP language.
- You are targeting Microsoft platforms only (it's worth remembering that Microsoft effectively cloned Java to create C# because they wanted a Java-like language that would lock people into Windows. They could have used Java, but that would have enabled people to easily run applications on other platforms....)
C# as a language is nicer than Java in various ways (better syntax for properties, value types, reified generics etc.). I prefer C# as a language to Java but in the grand scheme of things they are fairly similar languages and suitable for similar applications.
On the other hand, Java has some big advantages as well:
- Huge open source ecosystem - the libraries for Java that you can get for free are by far the best of any language. It is hard to overstate the importance of this - from the point of getting things done, Java is very effective.
- Tools - the Java tools are in my view better than what you can get the the .Net world. e.g. Maven (once you have mastered it!) is particularly impressive.
- Maintainability - Java has been around a while and has been successful in big companies precisely because it is relatively stable and there has been a lot of effort put into backwards compatibility. The simple and slightly verbose syntax also helps Java here - it's easier to read and maintain code if the code is very clear and explicit.
- New languages - The JVM has some amazing new languages (Scala, Clojure, Groovy etc.) that are the future of the Java platform. This is where much of the language innovation is happening, and it's happening much faster than in either Java or C#.
So Java vs. C# is a pretty close call and it really comes down to whether you want to be in the Microsoft camp or the Open Source / cross-platform camp.
Personally, I prefer Java because:
- The library ecosystem is in my view much more important that the fact that C# has nicer syntax than Java
- In the long run, I want all my code to be properly cross-platform and able to run on big clusters of cheap linux machines on the cloud.
- Clojure is IMHO the most promising language in the world right now, and if I stick with the JVM platform I'll be able to transition my code and skills easily into Clojure over time.
C/C++ is basically a different beast entirely. I would not recommend it for general purpose application development nowadays for the following reasons:
- Memory management - for most general purpose programming nowadays, you don't want to be managing your own memory. Garbage collection in C# or Java is much better for your productivity and sanity than any of the explicit memory management techniques you will have to use in C/C++
- Complexity - C++ in particular is an extremely complex language. It takes a long time to master and the code itself can also be fiendishly complex. (C++ templates for example are particularly hairy....)
- Productivity - most of the time and all else being equal it will take you longer to get things done in C/C++.
However it is undoubtably a great choice in a certain limited number of special domains, in particular:
- Operating systems - you probably want to use C/C++ if you are writing an operating system.
- Games development - nearly all the best commercial game engines are C/C++. It's still the best choice if you are developing a demanding AAA title (C# and Java are perfectly fine for less demanding / casual games)
- High performance computing - optimised C/C++ is probably the best way to create very high performance code. For most applications doing this level of optimisation isn't worth the effort, but in certain domains it can be extremely valuable (high frequency trading for example)
- Hardware access - You need direct access to the hardware (e.g. for an embedded system)
So basically, C/C++ is a great choice if and only if you are focused on one of the domains where it is particularly well suited.
I heard that syntactically they are almost the same.
Syntactically? Who gives a flying monkeys about syntax? Syntax is good for only one thing: allowing faster migration from syntactically similar languages. That's it.
C# is vastly better than Java. Consider their generic and functional programming support- C# is way ahead of Java. Not to mention operator overloads, and other good stuff- C# is vastly better featured. There's no way that Java could possibly be considered better than C#.
C++ and C# is more of a contest. C++ has an incredibly annoying archaic compilation model and a bunch of legacy diseases from C, but it's templates are vastly more powerful than generics, and it's resource managing approaches are much more flexible and powerful in general, as
using is a complete failure, and it executes faster.
C# has some nice built-in features like
LINQ and delegates. It is getting the best from both worlds -
C++. Look here for a full comparison.
But I like the
Java world better - much more open source frameworks and it runs on every platform. And don't tell me about
Mono - it is not a reliable option.
According to some sources (see e.g. http://www.indeed.com/jobtrends) C# is still less popular than Java, and as popular as C++.
C# does provide features that Java lacks, e.g. direct support for certain programming idioms like properties, functional programming style, and so on. C# has a higher level of abstraction than C++, which is an advantage when development time is more important that program speed.
Personally, I still prefer the Java / C++ worlds. As Petar Minchev said, Java has more open-source frameworks and applications, it runs everywhere, is less tied to a particular vendor and operating system. C++ has similar advantages, even though code often needs adaptations going from one platform to another. Since I prefer to develop on Linux and, to my knowledge, I cannot have fully fledged C# on Linux, I never got a real interest in C# because my programming needs are covered by C, C++, Java, Scala.
On the other hand, for many developers being tied to a particular vendor is not a problem: Microsoft has a dominant position in the operating system market and C# gives lots of job opportunities. Therefore, IMO many developers adopt C# because, besides being a feature-rich language, it is also a good investment.
What about "Which Software Development Framework, that includes a programming language" its better ?
You forgot to include other stuff, like the "environment" you will work with.
Are you going to work only for Windows O.S., but, doesn't have to be low level, and have a lot of memory and other resources ?
Choose .NET as framework over Windows, and use C#.
Are you going to work only for Windows, but, doesn't have to be low level, BUT, doesn't have a lot of resources ?
Choose the Delphi Framework (and Object Pascal Delphi programming language or Lazarus Object Pascal programming language)
Is your app. required to support several platforms, like a game, in different mobiles ?
Choose the Java Framework, and the Java programming language.
Is it Linux with KDE as graphic interface ?
Choose QT framework, with C++
Is it Linux with Gnome as graphic interface ?
Choose GObject/GLib framework, with C++
Are you going to work with a lot of low level operations, like developing drivers?
Plain C or C++ its used for several Operating Systems, with standard libraries, as a framework.
Just my 2 cents.
If you do a search you are likely to stumble upon discussion on top programming languages. Here is one of the search results - http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html - Java still seems to be the most popular language.
Java tried to remove some of the shortcomings of C++ (and making the programmers life simpler for non-real-time and non-critical applications). C# being the late-comer to the party, avoided some of the shortcomings of the Java language. C# has made a lot of progress (as Microsoft has lot of control over it) while advances in Java were blocked for a considerable period of time due to conflict between it's stakeholders.
A couple of things that have not already been mentioned:
C# is better than C++ because:
It does away with header files, which translates to great simplicity.
C# is better than Java because:
It supports both reference-type (class) and value-type (struct) user-defined types, which, if you know what you are doing, can yield significant performance benefits.
It supports delegates, which are like single-method interfaces, thus greatly simplifying the coding of frequently occurring constructs that involve single-method objects.
You should chose the best language for your expected environment and your expertise.
Chose C# if you are working in a Microsoft only environment. While C# is standardized under ISO/IEC 23270:2003, Microsoft's version remains the only complete implementation. Several key parts of the language are not covered by the standard and are thus subject to Microsoft's patents. No one else will implement a completely compatible version of the language for other systems, so in effect, you are vendor locked to Microsoft Windows and .Net for as long as you use the language. If you are looking for skills to use in the mobile market, best to look to another language.
Java works, but has a good deal of overhead, in part due to such features as garbage collection. Java is also not standardized by ISO/IEC, so you have no guarantees if you switch platforms and versions of Java, only Sun/Oracle's best intentions. If you are eventually planning to work with Android, this is definitely the way to go. Android's programming is basically Java, with a few changes.
C++ is standardized and almost all compilers follow the international standard, so you have guaranteed behavior BUT the language does not protect you from yourself. You have to perform cleanup and overflow checking on your own. This is not hard. C/C++ programmers have been doing these for many years. Apple uses Objective C for everything, so if you want to aim for Apple, I recommend you try this instead.
If you see yourself leaving Windows behind at some point, I'd suggest learning both C/C++ and Java - both of which are marketable at this time.
In relation to C++ vs C# (as i'm not proficient enough in Java), what i'm missing here is the ability to access lowlevel stuff on Windows. For example, you cannot develop a native display driver in C# (yet), but you can with C++. This does not make C++ better. I see C++ versus C# as Assembly versus C.
C# is in my view much more efficient if you look at the time it takes to actually implement a feature. The performance penalty of the .Net runtime is negligible for 99% of the applications developed. It may be of significance if you are running a tight loop, yes by all means, but most of the time an application is idle, waiting for any kind of input, signal or interrupt (disk IO, button click, network, animation completion).
The CLR library with all his functions has another big benefit. When i was training C# to junior developers, most of them said they liked the logical naming convention of classes, members and namespaces. Finding a feature of method was logical across the SDK, something that Visual Basic 5 was seriously flawed at. This has helped them tremendously in adopting the library. After learning the syntax of a language, getting to learn a new library is significant into getting a good grasp of any SDK. It saves you from reinventing the wheel.