C#C
C#2y ago
SWEETPONY

✅ why dispose call is the latest in the chain?

Actually I have two questions here:
  1. How actually duck typing works? Why I can do this:
    using var refStruct = new RefStruct();
    public ref struct RefStruct
    {
     public void Dispose()
     {
         Console.WriteLine("Hello from ref struct!");
     }
    }

    but can't do the same with class, class requires to inherit IDisposable and it is strange:
    using var simpleClass = new SimpleClass();
    public class SimpleClass
    {
     public void Dispose()
     {
         Console.WriteLine("Hello from class!");
     }
    }
2 question is: why dispose call is the latest in the chain? for example:
using var refStruct = new RefStruct();
using var simpleClass = new SimpleClass();
var factory = Factory.Create();
Console.WriteLine(factory);


and output will be:
Created Factory!
Hello from class!
Hello from ref struct!
Was this page helpful?