tags:

views:

505

answers:

8

A good acquaintance and past coworker called me up out of the blue and offered me a contract I really can't ignore. He's convinced I'll do fine with and pick up c# in no time ( past experience in -> c/c++ / PHP / Python / Lua ).

This question is in line with my two others:

PHP landmines in general

Python 2.x gotcha’s and landmines

A: 

Don't put ";" after a class definition !

In C++ :

class MyClass {


};

In C# :

class MyClass {


}
Amokrane
That's not something that would cause a bug... just a compiler error, and then he would learn.
Zifre
A: 

You should try ASP.NET MVC!

Frank Schwieterman
people have no appreciation for parody
Frank Schwieterman
You need to use an icon or something for humour.
ProfK
+7  A: 

What exactly do you expect people to answer here?

All the things you can trip up on when learning about C#? Truth to be told, there aren't all that many things. Sure, there's syntax differences from C, C++, Java and Javascript, languages that all look like C# but are wildly different.

However, the main portion of your time is going to be spent learning the .NET base class libraries, not the C# language.

Edit #1: Note that I'm not saying that there aren't "landmines" as that other question put it, in C# and .NET. All I'm saying is that, sure, I believe you should be able to pick up C# fairly quick as well, based on what you state you have past experience with. But I also say that learning C# isn't going to be the end-all, do-all, of your learning experience.

You can easily learn the difference between the manual shift and automatic shift systems of cars, but it will still take you some time actually using the system to its fullest.

Edit #2: Let me answer your question as you intended and add a typical pitfall you might encounter.

Enter: The aggressive garbage collector.

Consider the following code:

using System;
using System.Threading;
using System.Diagnostics;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread t = new Thread(GCThread);
            t.IsBackground = true;
            t.Start();

            SomeClass sc = new SomeClass();
            sc.Test();
            Console.Out.WriteLine("Ending");
        }

        private static void GCThread()
        {
            while (true)
            {
                GC.Collect();
            }
        }
    }

    public class Disposable : IDisposable
    {
        public Boolean IsDisposed { get; set; }

        public void Dispose()
        {
            IsDisposed = true;
        }
    }

    public class SomeClass
    {
        private Disposable _Collectable = new Disposable();

        ~SomeClass()
        {
            _Collectable.Dispose();
            Console.Out.WriteLine("Finalized");
        }

        public void Test()
        {
            Console.Out.WriteLine("Test()");
            Disposable c = _Collectable;
            Debug.Assert(!_Collectable.IsDisposed);
            Thread.Sleep(100);
            Console.Out.WriteLine("c.IsDisposed: " + c.IsDisposed);
        }
    }

}

The object, on my computer, is as follows:

Test()
Finalized
c.IsDisposed: True
Ending

Here, I construct an object of type SomeClass, and call the .Test method. This method, which is now running (I called it after all), grabs a copy of the internal variable _Collectable, which it then asserts that isn't disposed yet.

However, in the background, the garbage collector now runs (my background thread ensures GC runs as often as possible), which instructs SomeClass to finalize itself, even when our Test method is still executing, and the finalizer method disposes of the object.

Therefore, even when Debug.Assert makes sure that the IsDisposed property says false, when it comes to outputting it to the console, the object has been disposed, even while the method is still running.

The garbage collector is really aggressive, and even though I forced it to run as often as possible here, you can never guarantee when it will run. As such, this code shows a sample of the kinds of problem you can get into.

Lasse V. Karlsen
+7  A: 

Don't think of C# as C++ - it's a different animal.

Learn to use and love the GC. Expect to create more objects than you used to in C++ - minor temporary objects are very common. You're still static typed, but the language (especially using C# 3) will feel much more fluid and flexible when you start using lambdas, anonymous types, etc.

The biggest thing will be to learn and understand the .NET framework libraries. Understanding what's there will save you the most time, and also help you write code in a more "C#" fashion. If you need a crash course on making your code feel at home in C#, study the Microsoft Design Guidelines for Class Libraries.

Reed Copsey
+2  A: 

I think you'd find more 'gotchas' going the other way from c# to c/c++.

If you are comfortable with those languages already, you could probably do with reading through a textbook just to get up to speed on c# but I'd be amazed if you really had any problems.

Neil Trodden
+2  A: 

Understand the difference between lazy and eager evaluation in C#, or you may be scratching your head debugging LINQ queries. Here's a good link on the topic: Lazy Evaluation (and in contrast, Eager Evaluation)

Richard Hein
+1  A: 

I found Jon Skeet's "Bluffer's Guide"s useful:

http://www.yoda.arachsys.com/csharp/

Sinan Ünür
A: 

The main thing that flummoxed me was the lack of copy constructors and the reference versus value stuff.

Look up ICloneable, and Jon Skeet to the rescue!

It's pretty easy going to C# from C++ though. I don't think I could bear to go back.

Mark Simpson