Let's imagine the following situation:
public class A
{
private readonly Func<bool> _myFunction;
...
public A(Func<bool> myFunction)
{
_myFunction = myFunction ?? throw new ArgumentNullException();
}
}
public class B
{
private bool _myBool;
public bool MyBool => _myBool;
}
public class C
{
A a;
B b;
...
public void SomeFunction()
{
a = new A( () => (bool)b?.MyBool );
}
}
I would like to have the exception correctly raised in A
constructor since I want _myFunction
to be set to null if b
has not been instantiated.
Basically something like:
if (b == null) {a = new A(null);} else {a = new A(() => b.MyBool);}
I have tried using the null-conditional operator to do so, but I have being unlucky and I just create a reference to a function that returns null
.