I have the following code example taken from the code of a Form:
protected void SomeMethod()
{
SomeOtherMethod(this.OnPaint);
}
private void SomeOtherMethod(Action<PaintEventArgs> onPaint)
{
onPaint += MyPaint;
}
protected void MyPaint(PaintEventArgs e)
{
// paint some stuff
}
The second method (SomeOtherMethod) has resharper complaining at me. It says of onPaint that “Value assigned is not used in any execution path”.
To my mind it was used because I added a method to the list of methods called when a paint was done.
But usually when resharper tells me something like this it is because I am not understanding some part of C#. Like maybe when the param goes out of goes out of scope the item I added to the list gets removed (or something like that).
I thought I would ask here to see if any one knows what resharper is trying to tell me.
(Side Note: I usually just override OnPaint. But I am trying to get OnPaint to call a method in another class. I don’t want to expose that method publicly so I thought I would pass in the OnPaint group and add to it.)
The warning is correct. Consider the following:
Here the code modifies formal parameter x, and then never uses the modified x. This does not modify “this.X”
You’ve done the same thing with the delegate. You modify the formal parameter and then never use the result; the original “OnPaint” is unchanged, just as “X” is unchanged in my example.
Remember, just because a delegate is a reference type does not mean that you’re passing around a reference to a variable when you pass around an instance. You’re passing a reference to an instance, not a reference to the storage location of that instance.