Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to address co-variant array conversion?

Tags:

c#

I have an image stitching task that could take a lot of time so I run it as a seperate task like this

var result = openFileDialog.ShowDialog();
BeginInvoke(new Action<string[]>(StitchTask), openFileDialog.FileNames);

private void StitchTask(string[] fileNames)
{
    // this task could take a lot of time
}

Do I need to worry about the co-variant array conversion warning below or am I doing something wrong?

Co-variant array conversion from string[] to object[] can cause run-time exception on write operation

like image 446
jacknad Avatar asked Feb 15 '12 15:02

jacknad


1 Answers

Got it - the problem is that you're passing a string[] as if it were an array of arguments for the delegate, when you actually want it as a single argument:

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

Whatever's giving you the warning is warning you about the implicit conversion of string[] to object[], which is reasonable because something taking an object[] parameter might try to write:

array[0] = new object();

In this case that isn't the problem... but the fact that it would try to map each string to a separate delegate parameter is a problem.

like image 58
Jon Skeet Avatar answered Sep 22 '22 18:09

Jon Skeet