Lets assume I instantiated MyClass. Does my Foo property have a reference at this point or is it null? Also what happens after I call UseFoo() and exit the scope? Did I dispose Foo or just foo?
public class MyClass
{
private readonly string param;
private IDisposable Foo => new Foo(param);
public MyClass(string param)
{
this.param = param;
}
public void UseFoo()
{
using var foo = Foo;
// use foo
}
}
>Solution :
Q: Does my Foo property have a reference at this point or is it null?
A: No, because the property does not have a backing field, aka state.
Instead, every time you read the value of Foo, you will construct a new instance of the Foo type and return that.
As @juharr so nicely put it in their comment, it is a property but behaves like a method.
Q: Also what happens after I call UseFoo() and exit the scope? Did I dispose Foo or just foo?
A: You disposed the single object that you got from reading the Foo property and stored in the foo local variable.
Since the Foo property does not actually have state, you only disposed the object that was constructed for you when you read the Foo property.
The next time you read the Foo property you will get a new instance.