Can a compiler (for example, C #) automatically generate a call to the Dispose method for an object when it is set to null (of course, the object must support the Dispose method in the first place). For example, if we write
cnSqlConnection = null;
and cnSqlConnection is an instance of type SqlConnection, can the C # compiler insert an invocation of the Dispose method right before updating the null reference?
In addition, since framework classes support a scenario in which the Dispose method can be called multiple times, there would be no harm if the call is duplicated.
(a) . Dispose, , , .
Dispose
(b) . : - Dispose . , , CLR, .
, ::
class A : IDisposable { public void Dispose() { } }
1:
IDisposable a = new A(); IDisposable b = a; a = null; // The object is still alive in b, should it really be disposed?
2:
IDisposable a = new A(); IDisposable b = new A(); a = b; // a is not accessible anymore, but not set to null, //shouldn't it be disposed here?
3:
private void Foo() { IDisposable a = new A(); // a is not used any more, but not set to null, //shouldn't it be disposed here? }
# using, , :
using
using (IDisposable a = new A()) { // Do stuff } // Here a.Dispose() is automatically called.
, , , .
, , - , . , ( , , ).
, . GC , null ( ). , GCed, IDisposable, Dispose. CLR .
, ( ), COM, . - , GC- CLR.
-, : , , IDisposable, . , , , Dispose , ( ).
, . , , . .
, , , . . , , .
IDisposable() , . , , . , Dispose() . , COM-.
. , , . , . Microsoft , CLR. . GC .
, Dispose. , . , , .
.
void DoSomething(IDisposable disposable) { DoSomethingElse(disposable); disposable = null; }
, DoSomethingElse disposable DoSomething - ? , , , "".
DoSomethingElse
disposable
DoSomething
- , IDisposable Dispose, , . , , ? .
IDisposable
, , . , ? , , , . .
blog .
public static class MissingCompilerFeatures { public static void SetToNullAndDispose(ref IDisposable obj) { if (obj != null) { obj.Dispose(); obj = null; } } }
null , , Dispose . IDisposable, Dispose(), , using.
IDisposable garbage:
http://www.xtremedotnettalk.com/showthread.php?t=97910
Source: https://habr.com/ru/post/1761129/More articles:Page_ClientValidate () Question - javascriptМогу ли я получить Type() связанного объекта в С#/WPF (даже если значение bound равно null)? - reflectionlog4j conversion template for machine identification - log4jhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1761127/how-can-i-automatically-accept-what-git-rebase-interactive-presents-to-me&usg=ALkJrhhxWpWdMsp---P0be23BHeh8nZwnQNeed help with a LINQ to SQL WHERE clause on an external table - c #How to declare a variable in the header file to be used in two .cpp? - c ++https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1761131/what-are-good-default-items-to-add-to-hgignore-when-using-eclipse&usg=ALkJrhhkQ0pApH_6X66xpTvLoA_noJOmSQsed or awk to print lines between words - sedConvert text field text to integer - c #Why are my local dlls permanently loaded after installing _NT_SYMBOL_PATH? - c ++All Articles