It’s ridiculous I know… But heres a demo:
class Foo
{
// A lot of data in the nullable class foo.
}
class bar
{
public delegate void FooHandler(object sender, ref Foo f);
public event FooHandler FooChanged;
}
The problem is that I want to set ‘f’ to null in the event handler, but I can’t pass null as ref. I tried this instead:
public delegate void FooHandler(object sender, ref Foo? f);
But Foo is nullable, so that doesn’t work. I then tried modifying the invoke method for the handler and set up a dummy null item to pass, which didn’t work either. I tried using out instead of ref as out doesn’t need an initialised variable, and that compiled but didn’t run.
It’s really weird and I’ve never really seen anything like it. I could change the Foo class to be non-nullable so I can implement it as Nullable, or I could add my own flag Foo.HasValue and do it that way… But is there no way to do it without changing the Foo class?
This can definitely work, although it’s highly non-idiomatic… you say you’ve tried it and failed, but without the code, we won’t know what you’ve done wrong.
Ignoring the event part and the first parameter, both of which are irrelevant, here’s a complete example showing both
outandrefparameters working just fine: