Gotcha / surprises in C # .net 3.5

A good friend and former colleague called me out of control and offered me a contract that I really can’t ignore. He convinced me that everything will be fine, and I will immediately take C # (experience in → c / C ++ / PHP / Python / Lua).

This question matches my two others:

PHP land mines in general

Python 2.x gotchas and land mines

+3
source share
7 answers

What exactly do you expect from people here?

Anything you can use when learning C #? In truth, not all are that many. Of course, there are syntactical differences from C, C ++, Java and Javascript, languages ​​that all look like C #, but are completely different.

.NET, #.

# 1. , , # .NET " ", . , , , , , , #, , , . , # , -, .

, , .

# 2. , .

Enter: .

:

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);
        }
    }

}

:

Test()
Finalized
c.IsDisposed: True
Ending

SomeClass .Test. , ( ), _Collectable, , .

( GC ), SomeClass , , finalizer .

, Debug.Assert , IsDisposed false, , , .

, , , . , , .

+7

# ++ - .

GC. , ++. . , ( # 3) , , ..

- .NET framework. , , "#". - , #, .

+7

, "gotchas", # c/++.

, , , , #, , .

+2

Do not put it ";" after class definition!

In C ++:

class MyClass {


};

In C #:

class MyClass {


}
0
source

The main thing that confused me was the lack of copy constructors and value references.

Look at iCloneable and John Skeet for help!

It is quite easy to switch to C # from C ++. I don’t think I can return.

0
source

Source: https://habr.com/ru/post/1708781/


All Articles