What classes should I use a C # statement with?

I read, and I believe that I understand what the C # operator does using(please correct me if I am wrong): Initializes the IDisposable object as read only in a limited area (block using). I know that you can initialize to using, and this does not limit the scope, but is not recommended here:

http://msdn.microsoft.com/en-us/library/yh598w02.aspx

I do not always pay attention to which classes are subclasses of what. I'm not too sure which classes inherit from IDisposable. I'm not just interested in what classes can be used in the instruction using, but what classes did my colleagues expect to find in the block using? What classes should be in the block using? Also, is there really something wrong if you don't use a block usingand call Dispose? Is it just memory or stability?

+3
source share
10 answers

, , IDisposable , using. IDisposable , , ( , , ..), , .

, , , IDisposable :

  • . , using ( ) .
  • ( ), , . , Stream . IDisposable , , Dispose. - System.IO.StreamWriter
  • , "" . , IDisposable, , . - Dispose.

, , using. , , .

:

  • /

, IDisposable, , , , . , ; - , ? , - , ? , , , . , using, IDisposable.

Dispose, . Dispose. , , , , dispose , , - , . -, .

+16

. , , ..

, IDisposable, , , , , . (, , , ).

+13

Dispose , IDisposable, using.

. . .

StreamWriter - . Microsoft MDA, , Dispose. , : StreamWriter .

( , ), ... , , Dispose , , ( - ). using.

: Microsoft Management ESENT " ", , Dispose ( "" ).

, , , Dispose . .

, Dispose.

+2

, ,

+1

, Dispose, , / . - , Dispose , IDisposable.

+1

, , intellisense , . Image , System.Drawing, , ..

- . , , , , Dispose.

, .NET, IDisposable, , , , , . , ,

  • , (), ,
  • GC .

, , , - . .

+1

IDisposable . , , , . Dispose .

using Dispose, , , , .

using , , IDisposable. Try.. Catch.. Finally , Dispose finally, .

+1

IDisposable - , (, , ) . using

var disposable = new MemoryStream();
try
{
  //do some work with the disposable object
}
finally
{
  if (disposable!=null)
     disposable.Dispose();  
}

, , , , IDisposable... , , . , Fxcop, IDisposables, .

+1

, . doesnot IDisposbale, / . knwo, idisposable . , Idisposable.

0

-reckoner using:

  • ,

, Dispose, , , , .

- using dispose. - -, , , .

0

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


All Articles