views:

1535

answers:

18

I enjoy using Microsoft Windows and I've used C in the past and now I'm starting to learning C++, but it's going to take me some time to get proficient with it. I expect to write some simple applications and DLLs, not any low level stuff like 3D games, Operating System features or drivers. However C/C++ can be hard work with which to get code done right, see the What’s wrong with C++ compared to other languages? question.

I already have some useful C source code that I like and plan to use, although it's only somethings that run in a console window. There is a lot of C/C++ source available out there, to use or to learn from. Do I have to dump this if I start coding in C#, or do I carry on with C separately?

I don't know anything much about C# but maybe for example coding in it avoids the things I don't like about C/C++, e.g. having to spend energy worrying about memory management and re-inventing the wheel to do string handling, instead of just concentrating on the actual problem itself. It also seems to me that the previous programming languages and development tools from Anders Hejlsberg, lead architect of the C# programming language, have always been more of a pleasure to use than the other options available at the time.

I think that if I concentrate instead on learning C# I will enjoy the results of my coding sooner than with C/C++, however I will still have to know C in order to use the source code I already have access to.

On the other hand, the answers to C# in comparison to C++: what is your strongest pain? give me some encouragement to stick with C/C++.

+25  A: 

Yes, C# is likely to make you more productive than C/C++. In my experience there are fewer crufty bits, awkward parts of the language to be avoided etc. And yes, you can mostly avoid worrying about memory management, strings etc.

Things like lambda expressions, LINQ, iterator blocks also let you work at a higher level than C++ typically does - of course if you're a template/macro ninja you can do a lot of similar cool stuff, but it takes a good deal more expertise IMO.

The downside is that C# is not as portable as C++. There are projects like Mono but the vast majority of C# projects basically run on Windows in my experience. Mono will always have a hard time getting compatibility and keeping up with MS. The team does a fantastic job, but it is extra work.

Jon Skeet
What to do with the useful C source code that I already have - can it be easily ported to C#, or do I just separately carry on with C?
Rob Kam
That depends on the code. If it's doing lots of pointer arithmetic or Win32 stuff, it may be tricky to port. Plain algorithms may well be easy though. You may find you have a lot of "utility" code which is already in the .NET framework though :)
Jon Skeet
So far there's nothing specifically Win32. It's mostly string handling (using string.h), but there is pointer arithmetic.
Rob Kam
Downside is not limited to portability. A C# app requires a much larger footprint, put more pressure on the users when it comes to installation because of an additional framework.
Khnle
If it's done right, the pressure is not on the user, but the person implementing the installer. In a well crafted installer, the bootstrap program (usually setup.exe) will launch the .NET framework installer if needed then the installer for the app that needs it.
RobH
+4  A: 

As someone who was once on the fence myself, just switch and you'll thank yourself later. I had written C++ and Java all through college and always liked how simple Java was to get things done but I liked C++ better. C# was what I was looking for.

Just the simple fact that there is a huge class library is worth it. You don't have to either find some library to do it for you or write it yourself.

I do think the Windows only thing is a drawback but to be honest I never write anything but for Windows.

smack0007
+8  A: 

I agree with Jon, I'd like to add that C# is intended for Rapid Application Development more than C and C++ are. C# has very rich features such as LINQ, all the lovely Visual Designers you get with Visual Studio. There's a whole wealth of tools to help you. There are also some good C and C++ tools out there, but in my opinion, none as good.

C# is also a more modern language, the design team learned from mistakes of previous languages and have striven to make a more friendly usable language.

I have come from a C/C++ background, I am now a C# developer and don't want to go back :)

DoctaJonez
+1  A: 

It all depends on your goal. C/C++ are really system languages. They ARE designed to do messy, low-level things and are not that good unless you have a reason to use them (OSs would be one, for example) One would use C/C++ where performance and tight control over system memory are of prime importance.

For general development and programming, you have to switch to a higher level programming language like C#. You wouldn't want to implement a simple database or web application using C++! Believe me!

TheAgent
+1  A: 

Memory management is actually more of a chore in C# that C++ - I barely know of any memory leaks in C++ apps, I have seen a few but they've always been the kind of bugs that you know are so obviously bugs. In C#, you can end up leaking memory just by virtue of thinking it's being taken care of for you. (witness our latest work app, leaked 20Mb per job viewed. 20mb!!!)

Everyone is advised to run their C# app through a profiler like Nant, that's not advice people tend to give with non-managed languages.

Otherwise, you can continue to use both (though I'd recommend using C++ with the STL and perhaps Boost). Use C# for your GUIs (remember to unhook your event handlers afterwards!) and use C++ for your performance-heavy servers and you'll be getting the best of both worlds. I tend to use VB for my GUIs though, the change in syntax helps me stop making silly cross-over problems as C# looks too much like C++. There's no difference in the generated code, so there's no serious stigma to being a VB programmer anymore (or there shouldn't be, C# just being VB with curly brackets).

gbjbaanb
Every major C++ app I've worked on had memory leaks that we either couldn't understand, or understood but couldn't fix. And that's after spending 10% (a made-up number) of our dev time managing memory. Give me my GC.
Jay Bazuzi
You can't be too good then. All memory leaks are easy to fix - unless you have a spaghetti convoluted mess of a codebase. You're going to have a similar hard time with a GC.
gbjbaanb
Perhaps I am a poor programmer, with a weak mind, a soft mind, and a short memory. It hasn't turned me off coding, just turned me of C++. I'll take my cushy GC, and hope my employers don't notice that I'm not too good.
Jay Bazuzi
Interesting answer. There's an article about this trap for the unwary at http://www.codeproject.com/KB/showcase/IfOnlyWedUsedANTSProfiler.aspx
Rob Kam
+5  A: 

If you know C well, my suggestion would be to start using C++ "as a better C". That means start with simply recompiling your existing C code with a C++ compiler and fixing the compiler errors caused by the stricter type system of C++. The next step would be using templates and inline functions instead of macros, C++ style casts instead of C style ones, new/delete instead of malloc/free, C++ container classes instead of realloc. Later you can start using classes and even later class hierarchies.

As for C#, I used it for a while and then ran back to C++. For some applications (like web) C# is indeed a superior tool, but for the kind of programming I am interested in, it actually brings my productivity down. You'll need to make the decision for yourself, based on your particular situation, and not take other people's advice for granted.

Nemanja Trifunovic
It's possible that C++ will fix the things about C that I don't like, I'm still unsure. After reading the answers to http://stackoverflow.com/questions/370141/c-in-comparison-to-c-what-is-your-strongest-pain it seems likely that I'll be disappointed by C#. So for now I'll carry on with C++.
Rob Kam
+1  A: 

Learn in C++, then when you get serious and actually want to get work done, switch to C#. Unless there is something you actually have to use C++ instead for, C# will get it done faster. However, I still think a full C++ background is important so that one understands what is going on clearly even when managed code is taking care of it.

PhoenixRedeemer
+3  A: 

If I concentrate instead on learning C# will I enjoy the results of my coding sooner than with C/C++?

Yes. As has been pointed out before, C# is a more modern programming language than C or C++ and avoids some of things that make C/C++ painful. Here are some advantages:

  • automatic memory management - most of the time, you don't have to worry about memory leaks (there are exceptions to this rule, but mostly you only have to worry about objects implementing IDispose)
  • great tools - intellisense, and in particular resharper will save you so much time and stress, especially in a large project where there are lots of classes/a large API to keep track of
  • more readable code - the removal of pointers and template classes eliminate lots of "punctuation" characters (like * and <>) that can ultimately make the code hard to follow. Even compared to java, C# is syntactically very easy to follow.
  • LINQ gives you the option to use SQL style syntax on your data. Since SQL is so much more like natural language than typical programming languages (in that you describe what you want instead of specifying step by step how to do it), it can save you a lot of time fixing boilerplate loop-and-filter code.
+2  A: 

They aren't exclusive of each other. You can easily learn C# and C++. And Lisp and Eiffle and F# and Java and Perl and PHP, Python, Ruby and who knows what.

You probably won't be a master at any of them if you spread out, but you reach a point of diminishing returns on a single language anyway. I'd put that at about a year of professional use and maybe two years of hobby use. There are always new tricks to pick up, but you won't do that by staring at screens of code. Spreading yourself out into other languages will give you insight into new ways to do things, and ways to not do things. (PHP, cough, cough).

But for your question, yes learn C#.

It codes faster than C++, especially for GUI programs. I don't think C# is much, if any faster to write in than C++ if you were writing Unix CLI software or a network server. C# things like good threading support, events and properties and the integration into the IDEs are very nice for rapid GUI development. However, C#'s garbage collection isn't any better than C++ smart pointer wrapping and threads are a loss on Unix network services (use multiple processes and non-blocking, event triggered IO).

And another comment on GC: it's easy for the lazy, but you should know where your resources are anyway. NO ONE WANTS 50 MB auto-updater programs sitting in their notification bar. It may have had to read fifty XML and XSL files on start but that's no excuse to keep all that sitting in RAM.

Zan Lynx
+2  A: 

To answer your question a different way (which has been stated on other C# vs C++ threads), it really depends on what you want to do with the knowledge. Everything already stated here is correct: C# is better for GUI dev, it's higher level, easier to get into, harder to fix underlying issues, etc. However, personally, I think the #1 decision point for which language to pursue is what you want to do with it. C++ may be hell incarnate to you, but if you're looking to get a job and that's what the employer wants, that's what you should learn. Likewise you may think of C# as a constant process of copy/pasting and waiting for MS to produce working sample code to solve problems, but if it's the right tool for the job, learn it.

That's my 2c.

Nick
A: 

Well, I suspect I will get flamed for this, but somebody should say it. C++ is antiquated, outdated and obsoleate. One can logically question whether this thing ever really took off (in sooth).

C++ is like Delphi and Like VB.NET. It is a semi-object oriented and semi-procedural language. Most C++ programmers are actually, in sooth, C programmers who are somewhat familar with object orientation. Most C++ programs are actually C programs with very limited and selective use of the object oriented features of C++.

Java and C# are fully object-oriented languages. Everything is an object, period. Everything is a class, period. No equivocations. No Exceptions. No mercy.

Finally, the lesson we have been learning and rejecting over and over again during the past two decades is this: Once you go fully object oriented you must have automatic memory allocation and garbage collection. Manual memory management doesn't work well at all in a fully object oriented system.

When you get to the point where you are passing complex objects to other complex objects in constructors and methods, the lifespan of any given object becomes extremely uncertain. You don't know exactly when an object is done-dealing. It is hard to know when you can garbage collect and when you can't. If you collect one instant too early, you will get null pointer exceptions, and your app will shutdown. If you collect one instant too late, the memory never gets freed, and you have a memory leak. These are the two most ubiquitous errors in C++ history: The null pointer exception and the memory leak.

Both of these problems can be almost entirely avoided if you just code in C#.

David Leon
It is very hard to "flame" you because pretty much everything you wrote is incorrect. I.e.: In Java and C# not everything is an object, there is no "null pointer exception" in C++, you can have memory leaks in C#, etc, etc...
Nemanja Trifunovic
@ Nemanja True. And 'No Exceptions' aplies only to C (and 'No mercy aplies to every language ever created...) =)
Seiti
There are static methods in C#, not everything in C# is really an object.
tuinstoel
Java fully-OOP? The last time I checked you can play with int and Integer, the first being a primitive type while Integer is derived from Object. Java is an Object-based language (with classes based on a root object) not 100% OOP like ,e.g Smalltalk!
Hernán
Also, the reality is that more and more complex software in the industry, including 3D, CAD/CAM, medical imaging, high-end games etc. are still developed in C++ [even NEW projects for that fields]. So open your eyes, please. C++ is far from being obsolete and outdated.
Hernán
As I mentioned, it depends on your goals. I hate to say this, but there are goals you can't reach without C++ really.
TheAgent
+1  A: 

C# will serve you well in building Windows GUI, and more or less nothing else. C++ may be good for wider range of tasks (mostly high-throughput/high performance/low latency situations, in network programming, data streams, embedded programming, system programming), but Windows GUI is not one of them. Pick your specialization, and your language will follow.

(Yes, I know that C# is a decent language and should have wider use, but I just don't see anyone around using it for anything but front ends at the moment).

Arkadiy
I'd need more convincing to believe nobody is using it for anything but front ends at the moment.
Rob Kam
Here is a poll for you: http://stackoverflow.com/questions/403286/examples-solicited-who-is-using-c-for-anything-besides-windows-front-ends . To me, it seems that people mostly use it for front ends and Web Services, especially when it's already being used in other parts of the application.
Arkadiy
+1  A: 

You need C++ if you want to produce native code for a) high performance b) code without dependency on a huge .NET runtime etc. There is really no other choice.

On the other hand, if you are developing in-house or for other reasons have more choices about performance and deployment issues, you can choose between many languages which are more fun to code than C++, like:

-C# and Java, come with huge standard libraries, easy to program in and learn.

-Dynamic languages like Python and Ruby etc. which don't even need compilation and are way more flexible than C# or Java.

abababa22
A: 

In my belief, C++ is not an object-oriented language in a sense that the C++ language construct makes it hard to apply the "Design Patterns". It lacks the concept of the interface. You could argue Microsoft COM, but it's not a language construct.

Personally, I've met a smart Windows programmer who wouldn't learn C#. I wouldn't be in a position to hire him. ;)

yogman
Interfaces are conceptually easy to do in C++. You just don't have the keyword "interface". I don't see what's the big deal.
abababa22
What about interface type casting? You'll need QueryInterface() method and also AddRef(), and Release(). That's a big deal.
yogman
+2  A: 

I think that you can become a good C++ AND C# programmer, they are not opposites. Surely, you'll get more proficient in C++ if you are going for high-performance solutions, or multiplatform programming. Many people think that C++ cannot be used for end-user application programming, only for low-level "half-assed OOP" programs. With the excellent frameworks available for C++ application programming such as wxWidgets, Qt, VCL, etc, you can write top-notch applications with C++ quite fast.

Of course, C++ is hard to learn due to it's C roots, the lack of automatic memory management, many semantic pitfalls, etc. Scott Meyers says in his excellent "Effective C++" book that C++ is a "federation of languages", each one with it's own rules: procedural, OOP, generic and STL (which is in fact an application of generic programming). Add functional programming (some concepts being introduced in C++ 0x) and the absolutely amazing (but rather advanced) template metaprogramming and you've a bunch of stuff to learn.

C# is easier, everybody knows this. But don't get too confused by this: many programmers switch from bad C++ coding to C# screaming 'C++ is too hard' and start writing truly horrible code in C# instead... of course looking at their C++ friend and saying 'You see? This is faster and easier, isn't it?'.

I think that the cooperation of the two languages is perfect: you can write high-performance numeric algorithms in C++ and make it interact with your C# application which, for example, manages your weather prediction data in your databases, easily with LINQ.

So, take it easy, learn the language you want as long as you're open to learn how to do things properly.

Hernán
+1  A: 

It sounds like you have a general concept of programming style that matches C; you have some familiarity with the system libraries that are the means by which C (and C++) programs interact with their environment; and a modest amount of code that you'd like to reuse.

You need to know that you will be starting over, in more senses than you might anticipate, with C#.

The vast library of highly abstracted functionality that substitutes for the standard C libraries is a wonderful resource - they will be your best friends - but it's a crowd of strangers when you start out. Check out the documentation for "Console" for an example. It's not particularly difficult, but it's larger and different than a "console" in C.

Your existing code, if it does anything other than manipulate abstractions, will need to be reconceived and rewritten.

When you're all done, it will be more elegant, coherent, and reliable. But it will be less portable, larger, and ... different. As you read back through the responses already provided, notice all the unfamiliar acronyms and resources - each with a conceptual and logical background that won't map easily to what you already know. (But you'll get a little of that with C++ too, with streams io, etc.)

If you have enough time and interest, I think C# is the better ultimate destination. If you want to take the next step from where you are, it's not so obvious.

le dorfier
+1  A: 

I have switched from C++ to C# due to business requirement and I have to say C# is easier to start coding with and to create some good results.

As others have said, one of the strong area of using C# (which should say the .NET environment) is that you have automatic memory management which pretty much change how you are viewing your problems. In addition, C# doesn't use pointers which also helps me eliminate tons of potential errors. I always find it difficult to do pointer arithmetic by my brain.

However, as you have said you have some C code in place that reusing is a good idea. Even though C# can reuse code from a DLL, that may require a little effort to interat. In comparison, you can opt for managed C++ in which you can mix code with plain C/C++. That way, you can reuse all existing code and all the new code you created (ie managed C++ code) can still enjoy the benefits provided by the .NET environment.

Conrad
+1  A: 

Your end goal should be C#, but seriously learn C++ first. Its the foundation. C# and the .NET/Mono frameworks make everything so easy, and that's exactly the problem. If you just want to write web-services and User-Interface, you can skip C++. But you will always find yourself at a disadvantage to those who understand what's happening behind the scenes.