Applications Based on IDisposable, IEnumerable, and IEnumerator in C #

Source: Internet
Author: User

C # How to reasonably release the unmanaged memory? In this article, we will explain how to use IDisposable to release hosted and unmanaged memory.

  A.First, let the class implement the IDisposable interface, and then implement the IDispose method.

A.Core Disponse (bool isDisponse)

1. This method first checks whether isReadyDisposed is called for the first time. If it is not the first call, no operation is performed.

2. then determine whether it is a destructor call? If the Destructor call does not release the managed resources, it is released by the GC. If the GC is released before the managed resources are released, an exception may occur. This determines that the managed resource memory is released internally.

3. Release the unmanaged resources and set the flag isReadyDisposed = true.

  B.The memory is then released in two cases: release of managed memory and non-managed memory.

B.Release unmanaged memory

1. to release the unmanaged memory, you must manually call the Dispose () method of this class. This method first calls Dispose (true) to manually release the hosted and unmanaged resources, and then calls GC. suppressFinalize (this), so that GC does not call the destructor of this object.

B. BRelease managed memory

      1. the GC automatically calls the destructor to release the managed memory. The Destructor calls the Dispose (false) method internally. in this case, only the unmanaged resources are released, but the managed resources are released by the GC.

The class code is as follows:

Copy codeThe Code is as follows: public class IDisponseTest: IDisposable
{
Private bool isReadyDisposed = false;

~ IDisponseTest ()
{
// The managed resources are not released when the Destructor is called because they are released by the GC.
Disponse (false );
}

Public void Dispose ()
{
// Manually release managed and unmanaged Resources
Disponse (true );
// The user has released the managed and unmanaged resources, so you do not need to call the destructor.
GC. SuppressFinalize (this );

// If the subclass inherits this class, you need to write it as follows.
// Try
//{
// Disponse (true );
//}
// Finally
//{
// Base. Disponse ();
//}
}

Public virtual void Disponse (bool isDisponse)
{
// IsReadyDisposed is used to control the release of hosted and unmanaged resources only when Disponse is called for the first time.
If (isReadyDisposed)
Return;
If (isDisponse)
{
// The managed resources are not released when the Destructor is called because they are released by the GC.
// An exception may occur if GC has been released before the destructor releases managed resources.

// Release managed resources
}
// Release an unmanaged Resource
IsReadyDisposed = true;
}
}

C # create an iterator object? Use IEnumerable and IEnumerator

  First:Let the class inherit the IEnumerable and IEnumerator interfaces. In this case, the IEnumerable. GetEnumerator () method and IEnumerator. Current attributes, IEnumerator. MoveNext (), and IEnumerator. Reset () methods appear.

  Second:The IEnumerator interface is an object Traversal method and attribute implementation, while the IEnumerable. GetEnumerator () method is used to obtain the IEnumerator object.

  Finally:Let's take a look at the implementation of the iterator Code as follows:

Copy codeThe Code is as follows: class Program
{
Static void Main (string [] args)
{
CubeEnum cubelist = new CubeEnum (50 );
Foreach (Cube in cubelist)
{
Console. WriteLine ("cube Length:" + cube. Length + ", Width" + cube. Width + ", Height" + cube. Height );
}
Console. Read ();
}
}
// Cube, length, width, and height
Public class Cube
{
Public int Length {get; set ;}
Public int Width {get; set ;}
Public int Height {get; set ;}
}
/// <Summary>
/// Cube iteration set, inheriting IEnumerable and IEnumerator
/// </Summary>
Public class CubeEnum: IEnumerable, IEnumerator
{
// Index
Public int Index {get; set ;}
// Cube set
Public Cube [] cubelist {get; set ;}
// Initialize the cube set
Public CubeEnum (int count)
{
This. Index =-1;
Cubelist = new Cube [count];
For (int I = 0; I <count; I ++)
{
Cubelist [I] = new Cube ();
Cubelist [I]. Length = I * 10;
Cubelist [I]. Width = I * 10;
Cubelist [I]. Height = I * 10;
}
}
// Implement the GetEnumerator () method of IEnumerable to obtain the IEnumerator object
Public IEnumerator GetEnumerator ()
{
Return (IEnumerator) this;
}
// Current Cube
Public object Current
{
Get {return cubelist [Index];}
}
// Move to the next step
Public bool MoveNext ()
{
Index ++;
If (Index <cubelist. Length)
{
Return true;
}
Return false;
}
// Reset the index
Public void Reset ()
{
Index =-1;
}
}

This article describes the basic application of C #. If any errors occur, please correct them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.