Blackhole 1.0.1

dotnet add package Blackhole --version 1.0.1                
NuGet\Install-Package Blackhole -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Blackhole" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Blackhole --version 1.0.1                
#r "nuget: Blackhole, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Blackhole as a Cake Addin
#addin nuget:?package=Blackhole&version=1.0.1

// Install Blackhole as a Cake Tool
#tool nuget:?package=Blackhole&version=1.0.1                

Blackhole

A .NET library that provides a powerful way to void and dispose of any type of object, method, or class in C#. Like a real black hole, it consumes anything that comes its way, ensuring proper resource cleanup and memory management.

Installation

dotnet add package Blackhole

Blackhole is designed to be used with dependency injection, which is the recommended approach for several reasons:

  • Better testability of your code
  • Proper lifecycle management
  • Cleaner architecture
  • Easier maintenance

Here's how to use it:

// 1. Define your class with dependency injection
public class MyProgram
{
    private readonly IBlackHole _blackHole;

    public MyProgram(IBlackHole blackHole)
    {
        _blackHole = blackHole;
    }

    public void ProcessSensitiveData()
    {
        var sensitiveData = "secret information";
        _blackHole.Void(ref sensitiveData);
        // sensitiveData is now null
    }
}

// 2. Create an instance using the built-in factory
var program = BlackHoleProgram<MyProgram>.Create();

If you're using a dependency injection container (recommended for larger applications):

// Using Microsoft.Extensions.DependencyInjection
services.AddSingleton<IBlackHole, BlackHole>();
services.AddTransient<MyProgram>();

Features

  • Universal Type Support: Can void any type in C#
    • Value types (int, bool, etc.)
    • Reference types (string, objects, etc.)
    • Arrays and collections
    • Methods and delegates
    • Class instances
  • Automatic Resource Cleanup: Properly disposes of IDisposable objects
  • Memory Management: Forces garbage collection after voiding objects
  • Dependency Injection Support: Easy integration with DI containers
  • Thread-Safe: Safe to use in multi-threaded environments
  • Exception-Safe: Handles exceptions gracefully during disposal

Examples

Here are some examples of what you can void using the injected IBlackHole:

public class MyProgram
{
    private readonly IBlackHole _blackHole;

    public MyProgram(IBlackHole blackHole)
    {
        _blackHole = blackHole;
    }

    public void Examples()
    {
        // Void a string
        string secret = "sensitive data";
        _blackHole.Void(ref secret);
        // secret is now null

        // Void a value type
        int number = 42;
        _blackHole.Void(ref number);
        // number is now 0

        // Void an IDisposable object
        using var stream = new MemoryStream();
        var disposable = stream;
        _blackHole.Void(ref disposable);
        // stream is disposed and reference is null

        // Void a method
        Func<int, int> calculator = x => x * 2;
        _blackHole.Void(ref calculator);
        // calculator is now null

        // Void a class instance
        var myClass = new MyClass();
        _blackHole.Void(ref myClass);
        // myClass is now null
    }
}

Use Cases

  1. Security: Safely dispose of sensitive data
  2. Resource Management: Ensure proper cleanup of system resources
  3. Memory Optimization: Force cleanup of large objects
  4. Testing: Void objects and methods in test scenarios
  5. Cleanup: Remove references to unused methods and classes

Best Practices

  1. Always use dependency injection - Inject IBlackHole rather than creating instances directly
  2. Avoid static instances - Each class should have its own injected instance
  3. Always pass variables by reference using the ref keyword
  4. Use the using statement with IDisposable objects
  5. Consider implementing IDisposable for custom classes that need cleanup
  6. Void references to large objects when they're no longer needed

Thread Safety

The BlackHole class is designed to be thread-safe. Multiple threads can safely void objects simultaneously without causing conflicts.

Exception Handling

Blackhole handles exceptions gracefully during the voiding process:

  • Disposal exceptions are caught and suppressed
  • Reference nullification exceptions are caught and suppressed
  • Garbage collection exceptions are caught and suppressed

This ensures that the voiding process completes even if individual steps encounter issues.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 103 11/28/2024

upgrade