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?
egDoExport(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();
}
你可以传递一个什么都不做的动作:
DoExport((_, __) => { });
Or just handle it inside of the method:
private void DoExport(Action<ColumnView, bool> UpdateColumns)
{
if (UpdateColumns != null)
UpdateColumns(...);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.