C
C#8mo 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!");
}
}
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!");
}
}
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);
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!
4 Replies
canton7
canton78mo ago
It makes sense? If SimpleClass depends on RefStruct, it might be an error if RefStruct is disposed while SimpleClass is still alive
SWEETPONY
SWEETPONYOP8mo ago
ok I understand thanks what about duck typing?
canton7
canton78mo ago
What about duck typing? Ah I see what you mean ref structs can't implement interfaces, so we have to go down the route of duck typing there But classes can implement interfaces, so there's no need for duck typing
SWEETPONY
SWEETPONYOP8mo ago
hm interesting thanks for helping me
Want results from more Discord servers?
Add your server