Anhand eines Beispiels ist das denke ich am einfachsten zu verstehen. Ich schreib mir sonst die Hände fusselig für eine eigentlich eher simple Sache
Ich nehme mal Pseudocode lose orientiert an deinem frDrive Code.
C#-Quelltext
1: 2: 3: 4: 5:
| using (var volume = Drive.GetVolume('c')) { volume.Eject(); } |
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38:
| public static class Drive { public static IVolume GetVolume(char volume) { return new Volume(volume); } internal class Volume : IVolume { private readonly IntPtr handle; public Volume(char volume) { this.handle = myLovelyApiMethodForGettingAVolume(volume); } public bool Eject() { return myLovelyApiMethodForEjectingAVolume(handle); } public void Close() { myLovelyApiMethodForClosingAVolume(handle); } public void Dispose() { Close(); } } }
public interface IVolume : IDisposable { bool Eject(); void Close(); } |