Child pages
  • Co-variant array conversion may cause exception on write operation

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Redirect
locationhttp://www.jetbrains.com/resharperplatform/help?Keyword=CoVariantArrayConversion

Consider the following code listing

Code Block
csharp
csharp

class MyForm : Form
{
  ProcessFiles()
  {
    var ofd = new OpenFileDialog();
    ofd.ShowDialog();
    BeginInvoke(new Action<string[]>(Process), ofd.FileNames);
  }

  private void Process(string[] files)
  {
    // process each of the files
  }
}

...

The issue with the above code is easily resolved: since BeginInvoke() expects to receive an array of all the parameters and there is only one parameter (string[] files), all we have to do is provide exactly that, i.e.,

Code Block
csharp
csharp

BeginInvoke(new Action<string[]>(Process), new object[]{ ofd.FileNames });