I'm a python monkey at heart. I eat drink sleep and dream in it, and have found that it's taught me much more about writing quality code than my degree in Computer Science ever did, however I've been asked to write an automated testing project in C# at work to link in with Team Foundation Server.

Having looked at the language it looks very similar. I've noticed that it seems to be statically typed but I never had any problems with that in C++ or Ada. I've tried looking online for tutorials etc, but they all seem to start from the basics. I was hoping to be able to find information on differences and code translations et al similar to Norvig's lisp to python article.

A search on google hasn't rendered any results for me so I was hoping to have a bit more luck here.

As a little background I have about 4 years of python, 2 of C++, but that was a few years ago, and bits and pieces of knowledge of other languages. I try not to mention BASIC any more...
It is because of my extended current experience with python that I wish to compare C# with it.

  • Hi BiggAI -- did you ever find any resources for this question. I have essentially the same question and have been surprised how little out there on this there is, given the popularity of both Python and C#.
    – Ghopper21
    Oct 15 '12 at 9:25
  • I just found this compilation of tips and tricks, which seems useful.
    – Ghopper21
    Oct 15 '12 at 9:55
  • @Ghopper21 unfortunately not. It seems the philosophy of the two languages is very different. Python gives you a set of tools to craft your own solution, while C# seems to more give you a set of components that you slot together to come up with a solution. The result being that writing C# feels a lot less organic. The Zen of Python states that there should be only one obvious way to do it, this actually seems stronger in C#. My approach was to try to port a simple parser from python to C#, however on reflection, this was not a wise move - you're better starting from scratch on something new. Oct 15 '12 at 10:02
  • @Ghopper21 That list does look handy. The essay above is simply meant to mean, use the concepts you have learned from python, but don't expect to be able to do it the same way. Oh and expect to find patterns useful Oct 15 '12 at 10:05
  • Thanks for the advice. Here's a book I just found that seems like a more organized approach to the SO tips and tricks wiki.
    – Ghopper21
    Oct 15 '12 at 10:20

Just dive in.

I know both Python and C++, and I've had no problems in the past week or two picking up C#. My best advice is to do what I did: try to solve actual problems (as opposed to made-up test programs) with C#. If you can clearly express what you're trying to do, it's very likely that:

  • C# has a construct for what you want
  • Intellisense will probably tell you what it is
  • You'll be able to easily find detailed information on MSDN or google

Given my limited experience, it seems that C# is like most modern languages - the easy, clear way is usually the right way. If you find yourself writing overly verbose code, you're probably doing it wrong.

  • That's what I'm doing at the moment, however as I said above, I'm so familiar with python that I think in terms of its syntax. I think in terms of helping the garbage collector, indentation as code blocks, dynamic typing, dictionaries as the solution to any problem (as long as it can't be solved with a list). A normal tutorial will help me learn the language, but it won't stop me from trying to write python code in C#. Some sort of guide as to the differences etc however will help me to write C# in C# and python in python... Mar 24 '11 at 14:43
  • @BiggAl, I really don't think good python code will make particularly bad C#. The compiler will complain every time you forget curly braces. Maybe you shouldn't use the var keyword at first to force yourself to connect a variable and its type. And FWIW, I've solved pretty much every problem so far with some flavor of ICollection (usually generic SortedList) and a foreach loop, so you're not alone there. Mar 24 '11 at 15:06
  • How about things like scoping and garbage collection? How do they compare? I have to admit the main thing I'm having trouble with at the moment is argument passing (I'm used to everything as a reference, however this seems to work differently in C# leading to having to use values too. Now I have two problems...) Mar 24 '11 at 15:20
  • 1
    @BiggAl, objects aren't passed by reference. References (and everything else) are passed by value. If a variable is not a primitive type or a struct, it's a reference. As for scoping, from what I can tell a variable is limited to the scope in which it is declared, with the extra catch that inner scopes are children of outer scopes. So you can't have to declarations of the same name anywhere in both a parent and child scope (I think this is different from C++). As for GC, I have no idea. :-) I hope somebody is cleaning up after me. Mar 24 '11 at 15:27
  • And when an object reference passes out of scope is the object collected? (assuming no other references to that same object exist that are in scope in any way) I know a lot of my newbie bugs in python disappeared as soon as I figured out whet was magically being done to my variables... Mar 24 '11 at 15:57

I think you are better off looking at this as coming from C++ to C# (as you mention you have used C++) and leveraging that experience. Trying to apply your python knowledge to C#, especially now it has the dynamic type, could lead to some really nasty code (although there are those that would disagree).

With a C++ background you should steam through the basics, but they are still worth doing. You are learning a new language and you should treat it as such. You aren't hopping to a closely related language so just needing a differential.

  • I know the basics in C++, however from what I'd heard C# is more like Java only with C syntax? For instance instead of using lots of pointers it is considered better to use Abstract Base Classes, like python. I appreciate that I'm learning a new language but I really don't need to learn what a for loop is again, or any of the other related bits and pieces that just about every modern OO language has. Maybe this explains a little better what I'm looking for. Mar 24 '11 at 10:09
  • Agreed, basic constructs will be basic constructs in every language. I'm not a python person so can't comment on the similarities or differences, it just strikes me that if there were similarities that would allow a simple transition then it would be everywhere.
    – Lazarus
    Mar 24 '11 at 12:02
  • which language would be everywhere? In certain ways they both are, just in their niches. I just looked at the Microsoft knowledge DB for C# dynamic types - they do look nasty compared to the implementation in most dynamically typed languages... Mar 24 '11 at 13:45
  • Sorry, I wasn't clear. I meant there would be articles/guides everywhere on transitioning from Python to C# if it was a reasonable path where highlighting the differences would be easier than a 'clean slate' approach. I came to C# from C/C++ and even years later I still find things about C# that are elegant and faster than the approaches I brought with me from my visually similar known languages. In a way I wish I'd come to it without any familiarity so that I learned how to write C# and not C/C++ in C#.
    – Lazarus
    Mar 24 '11 at 13:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.