Skip to content

Instantly share code, notes, and snippets.

@sampletext32
Created November 27, 2020 17:50
Show Gist options
  • Save sampletext32/7d8c294a35a32801de341bfd08cc5f6d to your computer and use it in GitHub Desktop.
Save sampletext32/7d8c294a35a32801de341bfd08cc5f6d to your computer and use it in GitHub Desktop.
Interesting behaviour of a disposable struct
using System;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Disposable d = new Disposable();
try
{
Console.WriteLine(d.Disposed);
}
finally
{
((IDisposable)d).Dispose();
}
using (d)
{
Console.WriteLine(d.Disposed);
}
Console.WriteLine(d.Disposed);
}
}
struct Disposable : IDisposable
{
public bool Disposed { get; set; }
public void Dispose()
{
Disposed = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment