I have a struct TableRow and an array table of such structs:
table = new TableRow[10];
Now I want to find and manipulate the content of one of the structs in the array.
I use Array.Find to search the right array element:
var tr = Array.Find( table, tRow => tRow.color == 'red' );
tr.count++;
Problem is that structs are value types and so the Find method returns a copy of the struct that I want to manipulate. Changing this copy does not change the struct in the array.
I can’t seem to find any way to instruct the Find method to return the struct by reference.
You can’t change the way
Find()works for value types. You can either convert yourstructto aclass— typically we only usestructfor very small (<= 16 byte) value types that represent a “single” value anyway. MSDN has good guidance onstructvsclasshere: Chossing Between Classes and StructsOr you can use
FindIndex()to get the position in the array, and then use that to modify the field.