UpdateColumns) { ... ...">

How to pass the action "null"

I have the following function definition

private void DoExport(Action<ColumnView, bool> UpdateColumns)  
{  
...  
}

private void UpdateNonPrintableColumns(ColumnView view, bool visible)  
{  
...  
}   

An example of its call:

DoExport(UpdateNonPrintableColumns);

My question is. How to pass action "null"? Is it possible?

eg.
DoExport(null); <- Throws exceptionaffairs>

DoExport (null) throws an exception that is thrown when an action is called in the function body

+4
source share
3 answers

Skip the empty action if you want:

DoExport((x, y) => { })

Secondly, you should look at your code, as you pass null, if that’s fine.

public void X()
{
    A(null);
}

public void A(Action<ColumnView, bool> a)
{
    if (a != null)
    {
        a();
    }
}

Or according to C # 6 (using the zero propagation operator):

public void A(Action<ColumnView, bool> a)
{
    a?.Invoke();
}
+10
source

You can pass an action that does nothing:

DoExport((_, __) => { });
+10
source

:

private void DoExport(Action<ColumnView, bool> UpdateColumns)  
{  
  if (UpdateColumns != null)
    UpdateColumns(...);
}
+2

Source: https://habr.com/ru/post/1542619/


All Articles