Placing your code inside a using block ensures that it calls Dispose() method after the using-block is over, even if the code throws an exception.
someClass sClass = new someClass();
try
{
sClass.someAction();
}
finally
{
if (sClass != null)
mine.Dispose();
}
is same as
using (someClass sClass = new someClass())
{
sClass.someAction();
}
The .Net Framework provides resource management for managed objects through the garbage collector - You do not have to explicitly allocate and release memory for managed
objects. Clean-up operations for any unmanaged resources should performed in the destructor in C#. To allow the programmer to explicitly perform these clean-up activities, objects can provide a Dispose method that can be invoked when the object is no longer
needed. The using statement in C# defines a boundary for the object outside of which, the object is automatically destroyed. The using statement is exited when the end of the
"using" statement block or the execution exits the "using" statement block indirectly, for example - an exception is thrown. The "using" statement allows you to specify multiple resources in a single statement. The object could also be created
outside the "using" statement. The objects specified within the using block must implement the IDisposable interface. The framework invokes the Dispose method of objects specified within the "using" statement when the block is exited.
Note that the using Directive in C# is different from the using statement. The "using" Directive is used to provide an alias for a namespace.
None
0 Points
9 Posts
C#.net
May 08, 2018 02:21 PM|Puspa@007|LINK
Participant
830 Points
273 Posts
Re: C#.net
May 08, 2018 04:08 PM|Khanna Gaurav|LINK
Refer following link to know about using statement with example
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement
http://dotnetpattern.com/csharp-using-statement
Star
8119 Points
2778 Posts
Re: C#.net
Jun 21, 2018 02:28 PM|vahid bakkhi|LINK
hi
Placing your code inside a using block ensures that it calls Dispose() method after the using-block is over, even if the code throws an exception.
The .Net Framework provides resource management for managed objects through the garbage collector - You do not have to explicitly allocate and release memory for managed objects. Clean-up operations for any unmanaged resources should performed in the destructor in C#. To allow the programmer to explicitly perform these clean-up activities, objects can provide a Dispose method that can be invoked when the object is no longer needed. The using statement in C# defines a boundary for the object outside of which, the object is automatically destroyed. The using statement is exited when the end of the "using" statement block or the execution exits the "using" statement block indirectly, for example - an exception is thrown. The "using" statement allows you to specify multiple resources in a single statement. The object could also be created outside the "using" statement. The objects specified within the using block must implement the IDisposable interface. The framework invokes the Dispose method of objects specified within the "using" statement when the block is exited.
Note that the using Directive in C# is different from the using statement. The "using" Directive is used to provide an alias for a namespace.
Please MARK AS ANSWER if suggestion helps.