tags:

views:

28

answers:

3

What is mean by managed and unmanaged resource in dot net? how they comes in picture ?

+1  A: 

Managed resources are those that are pure .NET code and managed by the runtime and are under its direct control.

Unmanaged resources are those that are not. File handles, pinned memory, COM objects, database connections etc.

Oded
+1  A: 

The term "managed resource" is usually used to describe something not directly under the control of the garbage collector. For example, if you open a connection to a database server this will use resources on the server (for maintaining the connection) and possibly other non-.net resources on the client machine, if the provider isn't written entirely in managed code.

This is why, for something like a database connection, it's recommended you write your code thusly:

using (var connection = new SqlConnection("connection_string_here"))
{
    // Code to use connection here
}

As this ensures that .Dispose() is called on the connection object, ensuring that any unmanaged resources are cleaned up.

Rob
+1  A: 

The basic difference between a managed and unmanaged resource is that the garbage collector knows about all managed resources, at some point in time the GC will come along and clean up all the memory and resources associated with a managed object. The GC does not know about unmanaged resources, such as files, stream and handles, so if you do not clean them up explicitly in your code then you will end up with memory leaks and locked resources.

For more details - http://bytes.com/topic/c-sharp/answers/276059-what-unmanaged-resources

Sachin Shanbhag