from C++ to C# - c#

What are good c++ to C# articles/tutorial or books?
I am reading http://msdn.microsoft.com/en-us/magazine/cc301520.aspx and will read http://andymcm.com/csharpfaq.htm and http://www.ondotnet.com/pub/a/dotnet/2002/02/11/csharp_traps.html afterwards. Have any of you read any good books for ppl who have a strong understanding of programming && || C++ on the C# language (and libraries)?

It would be helpful to have the C# Language from Microsoft (not ECMA) specification with you to guide you with the syntax changes between C++ & C#.
download C# Version 3.0 Specification (.doc) here

Absolutely the best place to find information about C# is MSDN.
I recommend you follow the C# tutorials listed here: C# Tutorials on MSDN
I was also a C++ dev before I started doing C#. Those tutorials will get you up to speed.
Read about .NET as well.
Also, I forgot to mention the specification:
C# Language Specification on MSDN
It's worth reading through so you know some of the not-so-obvious differences between C# and C++ (for example, structs are pass by value whereas classes are pass by ref), and usage of 'out', 'ref', and other keywords. Just an example :).

I am currently reading Jon Skeet's C# in Depth. It is the best treatment I have seen for C# 2 and 3. If you are an advanced C++ programmer and you have some familiarity with C#, this is the book to take your C# competence to mastery.
Jon provides insights into the evolution of C# and insights into the various new language features. Not just the "hows" but also the "whys". Again, approach it after reading a beginning C# book. It doesn't cover the basic stuff which is covered many times by numerous other C# books.

I'll second the suggestion for CLR via C#, it is without doubt one of the de facto books one would expect any competent C# programmer to have read. Also, coming from a C++ background it will answer your questions about how things work in a managed environment.

I often find that personal experience provides the most insight into language differences. One can read all the theory written about a new programming language, but without experience this doesn't help much.
I'd recommend downloading Microsoft Visual C# Express Edition (http://www.microsoft.com/express/vcsharp/) and trying it out with a few simple programs. Start with "Hello World!" and work your way from there. (I know this can be tedious, but it helps to build a solid grounding in a language you are unfamiliar with). Once you have a solid feeling for the language, you should be able to focus your efforts (and questions) toward more specific language differences.

I recommend Accelerated C# book for programmers who's came from C++ and Java Languages.

Personally when I had to learn C# I would type the name of what I was looking for in the C++ term and tack on C#. For example:
std::vector C#
which gave me several pages dedicated to explaining lists and other types in C# that did what that exact code did in C++. Worked fairly well, even for some of the lesser used parts of the STL/Templating library.

If you're coming from C++, I think the best book to start with is CLR via C#. It will give you both a solid introduction to C# while also acclimating you to life in the CLR. The latter may be more of a stumbling block for someone with a solid C++ background.

Related

ASP.NET - C# vs VB.NET - Indirect differences and things you might not initially consider

I'm not interested in starting another "who has the bigger member" VB vs C# debate (https://stackoverflow.com/questions/158229/what-are-the-pros-of-vb-net seems to cover that already) though I am interested in indirect differences which may influence developing in one vs the other. All my commercial .NET development was desktop apps in VB until the last 3 months where I had a web project and decided it was a good opportunity to force myself to learn C#. In the course of doing so I've noticed a few non-technical differences:
A lot more mature open source apps and thorough examples are available in C# than for VB.
Third party vendors of add-ins for things like refactoring and documentation tend to support C# better than VB (if at all), with VB support for similar features from comparable C# versions often lagging behind or absent.
ASP.NET jobs targeting C# seem to pay around 15-20% more on average than otherwise identical jobs in VB (at least in Australia, looking on seek.com.au and careerone.com.au for reference).
The jump to ASP.NET, MVC and C# presented a lot of speed humps at once but I think was well worth it. The decision I need to make now is whether to put more energy into pursuing C# for future .NET development or if I might as well stick to VB. Are there any other points of difference between the language (other than direct language feature comparisons already covered) that one should consider in this instance?
I find that VS does more automatic code completion for VB than C#.
It's very useful to know both languages and I personally have to get better acquainted with C# so that I'm comfortable applying for C# jobs which should triple my options.
At the Microsoft 2009 Mix Web
developer conference, all the
presentations that I attended
included code examples in C#, not VB.
In StackOverflow, notice how
questions tagged c# largely outnumber vb.net and
vb.
John Skeet wrote C# in Depth, not VB in Depth.
One thing that has been widely stated when I have participated in hiring (both as a hiring manager, as well as a candidate) is that C# and Java are close enough that converting a candidate from one to the other is fairly easy, and that this is not true for a VB.NET candidate to C#, or Java. This statement has even been extended to C++, saying training a C# or Java developer (to code in C++) is easier than a VB.NET developer.
Though I tend to disagree (I think it is more about how the person sees programming in general) I know that several large, reputable firms will accept candidates for C# or Java jobs that list C++, C# or Java experience. These same firms will in general not look at candidates who only list VB.NET, or list VB.NET as their primary language.
Specifically for web development, becoming proficient in C# made writing javascript (and doing complex things using jQuery) much easier because the syntax is so similar.
I agree with your points, particularly the first. I would add that it seems there are more C# developers out there than VB.NET. If you are looking to hire another developer, you may get more (quantity or quality) from the C# pool. And employers, perceiving that C# is more popular, will go in that direction, and there will be more demand for C# programmers (hence the higher salaries/rates you noticed)
I know that this is an old post but I'd like to add one other factor: all the new stuff gets done in C# first. This comes from my experiences with early WPF development. All the examples where in C# and we were working in VB.Net. It just took extra time to translate. Another example is lambdas, initially you could use expressions (single line) or statements (multi-line) in C# but only expressions in VB.Net.
When .Net was first rolled out, I think that there were a lot of VB6 folks who didn't really get .Net and that it was a whole different animal. I think that those people were partially responsible for the negative perceptions of VB.Net. But that was 10 years ago now and no longer a factor.

Learning C# as a VB.NET programmer

I am looking to switch to C#. I come from a C / C++ background but C# is a little different for me. I am switching mainly because of Silverlight and MVVM; all examples are in C#. Any information is in C# and I am confused when I read through it all.
What is a good place to start learning C# when starting with existing C / C++ knowledge?
Start writing code in C# ASAP. Only way to learn the language is to write it. You already understand VB.NET. You've got a huge lead on anyone new to .NET.
Take a smallish application you've written in VB.NET and methodically change it over to C#.
You'll learn the syntax, etc. quickly.
Write all of your future projects in C#. Just force yourself into the change and you'll do fine.
Read the best C# books. :)
The Wikipedia article "C Sharp (programming language)" is a decent introduction to C# as a language.
Shortly after learning VB.NET I decided I'd rather be coding C#. I learned it by reading a couple of books and working on a project. Slowly, I got my coworkers converted over to the language from VB.NET as well. The languages are very similar, some code conversion is just a matter of changing syntax.
However, if you've ever worked with a true VB'er you will see VB specific functions used in their code that'll make you scratch your head. There are string handling functions and casting functions for example that you will have to convert. Also, C# has no RaiseEvent. Events take a little more effort to understand in C# because you need to wrap your mind around delegates (C++ function pointers/callbacks). As others have said, if you are coming from C++ this shouldn't be too difficult.
Many people go on about needing to work on a project to understand a language, which I agree with. However, there are features of languages which you will never know about if you don't pick up a couple of books and read about them. I've been able to show my coworkers many useful techniques just by reading a couple of books.
Lastly, C# is a great language. I was very skeptical when I first started using .NET, but now I know that MS got the right talent together for that framework and language.
Good luck.
As you know C++ (as you wrote in the question) and VB.Net (as you mentioned in the title), C# won't be hard for you if do know them both well. If this is the case, just grab any source in C#, and read it.
By the way, the article from wikipedia is a very nice intro.
One thing that may work for you is to use a converter
Write your desired code in VB.
Paste small pieces of code you don't know how to write in C# into http://converter.telerik.com/ and convert.
Manually type the result into your C# project.
Gradually, reduce your reliance on converter as you become more familiar with language.
That is enough to get you started, however C# has language features that VB does not, or VB offers in a way that is so wordy that no one really uses it.
The more features you will come across as you look at code examples from around the web (like on stack overflow), and you need to either look up syntax or function manually, or convert from C# back to vb to understand what it is doing (use same converter). Or, just paste it in and see what it does.

How do I move from Java to C#?

I know Java well. Which caveats and resources will help me cross to the other side (C#) as painlessly as possible.
Biggest tip: go with the .NET naming conventions from the word go. That way you'll constantly be reminded about which language you're in. (Sounds silly, but it really is helpful.) Embrace the idioms of the language as far as possible.
There are various books specifically for folks in your situation - search for "C# for Java" in Amazon and you'll get plenty of hits. It's worth reading carefully to make sure you don't assume that things will work the same in C# as in Java. (For instance, in C# instance variable initializers are executed before the base class constructor body; in Java they happen after. Subtle things like this can take a while to learn, and are easy to miss if you're skimming.)
If you're going to be using C# 3, I'd get a book which definitely covers that - everything in C# 3 will be new to you. Gratuitous plug: my own book (C# in Depth) covers C# 2 and 3, but assumes you already know C# 1. (In other words, it won't be enough on its own, but you may want it as a "second" book.)
See this great article on C# from a Java Developer's Perspective. It has several insights on the things that can be done in both sides to avoid minimum overhead. Having example in both the language you know and the language you want to learn eases the learning curve quite a bit.
Install Visual Studio 2008 and Resharper with IntelliJ IDEA key bindings. This gives you things like prompting you to include namespaces if you start using them.
Start a new project and start writing Java code, when you run into something that doesn't work properly or it's unable to find the class you're trying to use Google "PrintLn in c#".
Write tests or code snippets for sanity checks, like you may want to check if == works for strings (it does)
realize that c# alias Data Types (int is an alias for System.Int32, string for System.String)
look at other peoples code I recommend JP Boodhoos Google code
Take a job in C#, there's lots of jobs requiring both Java and C# especially in support.
Know your libraries, most Java libraries have been ported and most of the time the name is either like (Hibernate => NHibernate) or (Xstream => Xstream.Net). Not every library has an obvious name so just start looking into random ones you hear about here. ie (Rhino.Mocks,HTMLAgilityPack,MBUnit,Rhino.Commons,Castle Project)
Go to usergroup meetings look for a DNUG (Dot Net User Group) they'll be helpful and you can get some good advice.
There's a cheat-sheet from Microsoft for Java developers using C# :)
I know that a good answer has already been accepted. However, I'd like to make an addition...
I find that learning a new language typically involves learning subtle syntactic differences....especially when dealing with the difference between languages in the C/C++/Java/C# family.
In addition to a nice thick reference book I recommend getting a pocket reference like C# 3 Pocket Reference from O'Reilly. It won't help you with the design patterns etc...but will provide a very quick reference about the specific differences of the language you are using.
Here's a quick blurb about this book from that site:
C# 3.0 Pocket Reference includes plenty of illustrations and code examples to explain:
Features new to C# 3.0, such as lambda expressions, anonymous types, automatic properties, and more
All aspects of C# syntax, predefined types, expressions, and operators
Creating classes, structs, delegates and events, enums, generics and constraints, exception handling, and iterators
The subtleties of boxing, operating overloading, delegate covariance, extension method resolution, interface reimplementation, nullable types, and operating lifting
LINQ, starting with the principles of sequences, deferred execution and standard query operators, and finishing with a complete reference to query syntax-including multiple generators, joining, grouping, and query continuations
Consuming, writing, and reflecting on custom attributes
I used this book (well the original) to help me go from being a Java to a C# developer. While I was learning, I kept it by my desk at all times and it really helped.
I made the transition pretty easily by using C# at work, but one of the most important things to do is familiarize yourself with the .NET API and some of the powerful techniques available in C#.
After I learned the .net library I relied on it a lot more than I used to, so learning about the things it can do for you is very helpful. After that, if you work with db code at all, learn LINQ, and also techniques lambas, anonymous types and delegates are also a useful to pick up.
The language syntax is vary similar, so I should only read a small reference of the C# syntax. Like a simple book (for experienced programmers) or maybe wikipedia (http://en.wikipedia.org/wiki/Comparison_of_Java_and_C_Sharp) will tell enough.
The biggest difference is the library: Asp.Net websites are totally different from java servlets.
Don't read much, just start programming!
Here's a link that has syntax comparison between Java and C# (even though it's almost identical, there are a few differences).
http://www.harding.edu/fmccown/java1_5_csharp_comparison.html
Use Sharpen to convert your Java programs to C# and see the differences.

Learning C# after C++

In a progression of languages, I have been learning C and C++. Now I would like to learn C#. I know there are some drastic differences between them - such as the removal of pointers and garbage collection. However, I don't know many of the differences between the two.
What are the major differences that a C++ programmer would need to know when moving to C#? (For example, what can I use instead of STL, syntactic differences between them, or anything else that might be considered important.)
C# for C++ Developers is a great place to start. It is a table that lists the most important comparisons between the two languages.
Once you have explored some of these differences, you might choose a self-contained project you have written in the past in C++, and re-write it in C#. In your first pass, you will probably just end up translating directly across, using the same design and algorithms. As you become more comfortable with C#, you will recognize ways to take advantage of language features only available in C#, as well as the incredibly versatile .NET Framework.
I think it's important to note that pointers weren't really removed from C#, but that they still exist, they are everywhere, and they are called references.
Charles Petzold has a .NET Book Zero that is designed specifically for the C/C++ Windows programmer who wants to learn C# and .NET. It is a free PDF download from his site. All of the source code used in the book is available as a separate Zip-file download.
There was a similar question that has lots of pointers (pun inteded :-)) and resources about the differences between C++ and C# and what to pay attention when learning transitioning - Linux/C++ programmer to Windows/C# programmer.
Look at the Generic collections and LINQ (all the related technology to LINQ) for STL like functionality.
The number one thing to note is that ~/Destructor is c++ is nothing like the ~/finalizer in c# -- See IDisposable/using.

Linux/C++ programmer to C#/Windows programmer

I have been coding exclusively for a while now on Linux with C++. In my current job, it's a Windows shop with C# as main language. I've retrained myself to use Visual Studio instead of emacs ( main reason is the integrated debugger in VC, emacs mode in VC helps ), setup Cygwin ( since I cannot live without a shell ) and pickup the ropes of managed language. What tools, books, website ( besides MSDN ) or pitfalls do you think I should check to make myself a more efficient Windows/C# coder?
The first things to consider when switching from C++ to C# the fact that mostly share some of the surface syntax, but the difference of programming paradigms gets bigger and bigger as you dig in more into .Net.
Get to know the C# core programming paradigms before starting to program else you might fall in the trap of writing C++ programs in C#, which isn't the best idea by long stretch.
The most important things to get accustomed to are:
Automatic memory management and garbage collection including the dispose pattern. Learn the basics and pitfalls.
What are classes, interfaces, structs and primitives in .Net, and how they behave compared to C++.
Events, delegates, properties, lambda expressions all of which are somewhat new concepts when coming from C++.
.Net generics and differences between templates.
strings, arrays, custom attributes, reflection, exceptions and threading basics in .Net, all of these are heavily used everywhere in .Net, and you must learn their intricacies to use them effectively.
GUI programming in Winforms and ASP.Net (and maybe after there WPF), components, controtrols, databinding. The .Net GUI model.
First start with a generic .Net book that introduces you to all of these concepts. I recommend a book over reading tutorials and articles first of all so you can have a big complete picture of .Net at the end. Articles on the internet might not achieve this.
Best generic .Net book I've read:
Professional C#, 3rd edition. by Simon Robinson, Christian Nagel, Karli Watson, Jay Glynn, Morgan Skinner, Bill Evjen
And second, since you're from a C++ background, and you are used to working close to the metal and thinking in way that is close to how hardware works (raw memory management (pointers, mem allocations, etc) I can only recommend one book that will really demystify what .Net is and what it does :
CLR Via C# by Jeffrey Richter
I can't stress enough how good this book is for every .Net developer, especially when coming from C++ and at the same time being one of the best .Net books I've read. The book is a pure pleasure to read and covers topics from :
.Net execution model (from MSIL to native code)
Memory management (how the .net runtime and garbage collector manages memory, heap layout, memory generations, finalization, large object heap)
Designing types
Assembly loading, reflection, application domains
and many more ...
This is my best advice I could give to anyone on their way to become an expert C# developer in the shortest time possible.
Since you already know how to program in C++, check out:
A Programmers Introduction to C# 2.0, by Eric Gunnerson and Nick Wienholt
Nice balance between language reference and general .net information.
Similarly:
Essential .NET, Volume I: The Common Language Runtime by Don Box and Chris Sells
Really interesting "under the covers" net book (but written for .net version 1 so may be a little out of date).
My background was predominantly C/Unix and Python with some java and dusty 2000-vintage VB6 when I first used C#. I was familiar with managed runtimes from the work I had done with Java and the .Net API's have a somewhat similar look-and-feel to earlier MS API's.
I found Troelsen's Pro C# and the .Net 2.0 Platform to be a really good C#/.Net resource. There are more recent editions out now.
Check out petzold's dotnet book zero. This might help.
Read about garbage collection in .Net. People coming from C++ land are used to the explicit memory allocation and management. In C# explicit memory management is virtually non-existing.
Another topic you should check out is the difference between C# generics and C++ templates. I don't have a good link for that one, though.
Depending on the product you are working on, you might have to resort to calling Win32 API functions from C#. This is done through P/Invoke, so you might want to read a bit about it as well. And if you have to actually use it, http://pinvoke.net is very useful collection of C# declarations for most of the Win32 APIs.
You might also want to learn at least the basics of COM, as very often C#/.Net applications choose/need to reuse components from third party vendors, which are often implemented as COM components. COM is a complex topic though. My favorite books on COM and Essential COM by Don Box and Professional DCOM by Roger rimes. I would borrow these from a library, as all you need to read are the first few chapters (unless you want to go in depth).
A basic understanding of windows, messages and message queues is necessary, if you are going to write client applications. You will be using Winforms of WPF/XAML for these, and both technologies do a good job of isolating the details from you; however to be able to write good code you need to know what is going behind the scenes. I am not sure what a good book would be for that, but MSDN has lot of information.
Get yourself a copy of Resharper. It's probably the single best productivity tool out there for straight up coding.
Shouldn't be the base the CLR? And shouldn't it be unimportant which .NET language is used? If I look through the .NET docs I can decide if I'd like to see the stuff in VB.Net, C# or C++. So if you know C++ why shouldn't you use "managed C++"?
Regards
Friedrich

Categories