Do we have unmanaged resources in C #?

I had a discussion with my friend about managed and unmanaged resources in C #.

According to my friend:

1.a) Each object in C # is managed, and when encoding in C # nothing happens like an unmanaged object or resource. The concept of an unmanaged resource comes only with C ++.

1.b) Whether we have a managed or unmanaged resource in C ++, we need to explicitly free it. Since we have an automatic garbage collector in C #, we do not need to think about resource management.

As for me:

2.a) If we don’t have an unmanaged resource, then why do we need a finalizer or Dispose method in C #?

2.b) The garbage collector has information about distributed memory, and not about the status of resources. Therefore, we need to use the dispose method to free resources in C #.

I need help understanding which of the above arguments are correct, and information about an unmanaged resource in C #, whether they exist or not?

Thanks at Advance.

+5
source share
8 answers

No, it is impossible to write a program in C # without using unmanaged resources. This is inevitable, C # runs on an operating system that is 100% unmanageable. If you use a file, you are using an operating system resource. Network connection. Subject. Console. Etcetera, all very unmanaged resources.

, , .NET. framework - . FileStream, Socket, Thread, Console .. , .

. -. , , . , - , , , .

, . .

, . , . , , . , .

, . . , , . , , , , , . , , FileShare.None, . : Close(), , . , , Close() finally, , - . , .

, . , , , 100 . , , . . , , . , , , .

.NET IDisposable. Dispose() , , , , . , using , , IDisposable.Dispose() .

Dispose() , IDisposable, , , .NET-. , , . , Dispose() , MemoryStream. , IDisposable, , Thread. Dispose, Close ( ). , Java , IDisposable.

+6

, .NET, , rss.. (GC) , , , , . , , , , . , , , , API, CoAllocTaskMem.

, , , , . , Finalize System.Object, , ( # ++, MyObject, ). , , , , .

, , , , . , , . , 1 10 , , , finalize.

IDisposable, . .

+3

a) # , # , . ++.

. - # (, COM), .

, , " " # . , , , , . , :

class Foo
{
    private Thread thread = new Thread(new ThreadStart(DoLotsOfWork));
    private AutoResetEvent endThread = new AutoResetEvent(false);
    private int sum = 0;

    public Foo()
    {
        thread.Start();
    }

    public StopThread()
    {
        endThread.Set();
    }

    private void DoLotsOfWork()
    {
        while (!endThread.WaitOne(1000))
        {
            sum += 1;
        }
    }
}

static void Main(string[] args)
{
    Foo foo = new Foo();
    // Additional code...
    foo.StopThread();
}

, . StopThread, , DoLotsOfWork, , , , .

b) ++, . #, .

#. IDisposable , .

:

class Foo : IDisposable
{
    private bool disposed = false;
    private Thread thread = new Thread(new ThreadStart(DoLotsOfWork));
    private AutoResetEvent endThread = new AutoResetEvent(false);
    private int sum = 0;

    public Foo()
    {
        thread.Start();
    }

    public StopThread()
    {
        endThread.Set();
    }

    public Dispose()
    {
        this.Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void DoLotsOfWork()
    {
        while (!endThread.WaitOne(1000))
        {
            sum += 1;
        }
    }

    private void Dispose(bool disposing)
    {
        if (!disposed && disposing)
        {
            StopThread();
            disposed = true;
        }
    }
}

static void Main(string[] args)
{
    using (Foo foo = new Foo())
    {
        // Additional code...
    }
}

, , , , Foo, .

+3

, .net Framework, Managed code, memeory , . framework- .

, .net, - .

, hevy, , , , , .

+2

, , CLR, CLR, .

, , CLR (, COM- ), . CLR , IDisposable, , .

+1

.NET. .

. , Dispose #.

+1

Theres IDisposable

+1

, , (, ), , . , , .

- , , , , , "" .

Subscriptions from long-lived objects exist completely inside managed code, but since they are not automatically cleared during the life of long-lived objects, they should be considered as unmanaged resources.

+1
source

All Articles