Blackhole 1.0.1
dotnet add package Blackhole --version 1.0.1
NuGet\Install-Package Blackhole -Version 1.0.1
<PackageReference Include="Blackhole" Version="1.0.1" />
paket add Blackhole --version 1.0.1
#r "nuget: Blackhole, 1.0.1"
// 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
Usage (Recommended Approach)
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
- Security: Safely dispose of sensitive data
- Resource Management: Ensure proper cleanup of system resources
- Memory Optimization: Force cleanup of large objects
- Testing: Void objects and methods in test scenarios
- Cleanup: Remove references to unused methods and classes
Best Practices
- Always use dependency injection - Inject
IBlackHole
rather than creating instances directly - Avoid static instances - Each class should have its own injected instance
- Always pass variables by reference using the
ref
keyword - Use the
using
statement withIDisposable
objects - Consider implementing
IDisposable
for custom classes that need cleanup - 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 | Versions 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. |
-
net6.0
- Microsoft.AspNetCore.Hosting.Abstractions (>= 2.1.1)
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- System.Text.Encodings.Web (>= 9.0.0)
-
net7.0
- Microsoft.AspNetCore.Hosting.Abstractions (>= 2.1.1)
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- System.Text.Encodings.Web (>= 9.0.0)
-
net8.0
- Microsoft.AspNetCore.Hosting.Abstractions (>= 2.1.1)
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- System.Text.Encodings.Web (>= 9.0.0)
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