This method doesn't work at all:
public static void Delete(ref Prot[] pack, Prot prot)
{
var temp = new List<Prot>(pack);
temp.Remove(prot);
pack = temp.ToArray();
}
What am I doing wrong?
Thank you.
This method doesn't work at all:
public static void Delete(ref Prot[] pack, Prot prot)
{
var temp = new List<Prot>(pack);
temp.Remove(prot);
pack = temp.ToArray();
}
What am I doing wrong?
Thank you.
Your class Prot
needs to override the Object.Equals()
method. This is how List<T>.Remove
works. From the documentation:
If type
T
implements theIEquatable<T>
generic interface, the equality comparer is the Equals method of that interface; otherwise, the default equality comparer isObject.Equals
.
If you don't override Object.Equals
it will just use the default implementation which checks for reference equality, not value equality.
So, temp.Remove(prot);
was never removing any values. (This can be validated based on the return value of Remove
. It returns true
if it successfully removes a value, and false
otherwise.
Here's a basic example: http://ideone.com/vOZoYI
Initial Answer (issue was a typo in question).
You are modifying a new object, a List<Prot>
, not the Prot[]
parameter. If you assign pack
to the List<Prot>.ToArray()
then it will remove it from the array passed in.
public static void Delete(ref Prot[] pack, Prot prot)
{
var temp = new List<Prot>(pack);
temp.Remove(prot);
pack = temp.ToArray();
}