Table of Contents

Class Disposable

Namespace
Existential
Assembly
Existential.Net.dll

Provides methods to help prevent resource leaks by safely handling disposable objects and enumerators:

  • a utility method, SafelyReturn<T>, for creation and initialization of System.IDisposable objects in line with best practices, so that they can be safely returned from the calling method without violating CodeAnalysis error CA2000 .
  • overloaded extension methods (UsingNonGenericEnumerator) for collections accessed through the System.Collections.IEnumerable interface that help to ensure their System.Collections.IEnumerator is correctly disposed if it happens to be disposable.
public static class Disposable
Inheritance
System.Object
Disposable
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()

Methods

SafelyReturn<T>(Action<T>)

Creates and initialises an System.IDisposable object of type T using best practices so that it can be safely returned without violating Code Analysis rule CA2000.

public static T SafelyReturn<T>(Action<T> inInitialiser = null)

    where T : class, IDisposable, new()

Parameters

inInitialiser System.Action<T>

An optional System.Action that initialises a type T object after it has been created.

Returns

T

An initialised System.IDisposable object of type T that can be safely returned without violating CA2000.

Type Parameters

T

The type of the object to be created. It must be System.IDisposable and have a default constructor.

Remarks

This method follows best practices for creating and returning disposable objects, as outlined in the documentation of the CA2000 CodeAnalysis error .

Exceptions

System.Exception

The provided System.Action<T>, inInitialiser, could throw an System.Exception when invoked.

UsingNonGenericEnumerator(IEnumerable, Action<IEnumerator>)

Executes an action using the collection's non-generic enumerator. If the enumerator is disposable, it'll be disposed correctly.

The non-generic enumerator of a collection might be disposable. If so, it needs to be disposed correctly to prevent memory leaks. By controlling the lifetime of the enumerator (like a using statement), this method ensures that the enumerator is properly disposed of after the action has been executed.

public static void UsingNonGenericEnumerator(this IEnumerable inCollection, Action<IEnumerator> inAction)

Parameters

inCollection System.Collections.IEnumerable

The non-generic collection to apply the action to.

inAction System.Action<System.Collections.IEnumerator>

The action to execute on the collection.

Examples

This sample shows how to call the UsingNonGenericEnumerator(IEnumerable, Action<IEnumerator>) method.

theCollection.UsingNonGenericEnumerator(inEnumerator => { /* Your code here */ });

Exceptions

System.ArgumentNullException

An System.ArgumentNullException will A System.ArgumentNullException will be thrown if the System.Action<T> is null.

System.Exception

The provided action, inAction, could throw an System.Exception when invoked.

UsingNonGenericEnumerator<TResult>(IEnumerable, Func<IEnumerator, TResult>)

Executes a function using the collection's non-generic enumerator. If the enumerator is disposable, it'll be disposed correctly.

The non-generic enumerator of a collection might be disposable. If so, it needs to be disposed correctly to prevent memory leaks. By controlling the lifetime of the enumerator (like a using statement), this method ensures that the enumerator is properly disposed of after the function has been executed.

public static TResult UsingNonGenericEnumerator<TResult>(this IEnumerable inCollection, Func<IEnumerator, TResult> inFunction)

Parameters

inCollection System.Collections.IEnumerable

The non-generic collection to apply the function to.

inFunction System.Func<System.Collections.IEnumerator, TResult>

The function to execute on the collection.

Returns

TResult

The result of the function.

Type Parameters

TResult

The type of the result returned by the function.

Examples

This sample shows how to call the UsingNonGenericEnumerator<TResult>(IEnumerable, Func<IEnumerator, TResult>) method.

bool isCollectionEmpty = theCollection.UsingNonGenericEnumerator(inEnumerator => !inEnumerator.MoveNext());

Remarks

Using this method helps to resolve an inspection warning 'Return value of a method annotated with [MustDisposeResource] is never disposed' that may appear in the IDE when a non-generic Enumerator is accessed using GetEnumerator(). This method encapsulates the use of the non-generic enumerator and ensures that it's disposed correctly.

The [MustDisposeResource] attribute is part of JetBrains.Annotations , a library of attributes used to improve upon default code inspections.

Exceptions

System.ArgumentNullException

An System.ArgumentNullException will A System.ArgumentNullException will be thrown if the System.Action<T> is null.

System.Exception

The provided function, inFunction, could throw an System.Exception when invoked.