I have the following function definition
private void DoExport(Action<ColumnView, bool> UpdateColumns)
{
...
}
private void UpdateNonPrintableColumns(ColumnView view, bool visible)
{
...
}
Example of it being called:
DoExport(UpdateNonPrintableColumns);
My question is. How do I pass a 'null' action? Is it even possible?
e.g.DoExport(null); <- Throws exception
DoExport(null) causes an exception to be thrown when the action gets called in the function body
Pass in an empty action if you want to:
DoExport((x, y) => { })
Second, you have to review your code, since passing in null
is perfectly fine.
public void X()
{
A(null);
}
public void A(Action<ColumnView, bool> a)
{
if (a != null)
{
a();
}
}
Or as per C# 6 (using the null-propagation operator):
public void A(Action<ColumnView, bool> a)
{
a?.Invoke();
}
You can pass an action that does nothing:
DoExport((_, __) => { });
Or just handle it inside of the method:
private void DoExport(Action<ColumnView, bool> UpdateColumns)
{
if (UpdateColumns != null)
UpdateColumns(...);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With