Created
November 27, 2020 17:50
-
-
Save sampletext32/7d8c294a35a32801de341bfd08cc5f6d to your computer and use it in GitHub Desktop.
Interesting behaviour of a disposable struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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