D
Dave Harris
In the following code, the output is 1 instead of 7. I understand that the
generic list<> is generating a temporary copy of PointD, and then the Set()
call works on the temporary and so the result is lost. It's just subtle
enough though, that unless you are paying really good attention, it's easy to
miss. FXCop doesn't seem to catch it, not does resharper. Aside from
changing the struct to a class, other ideas? Thanks.
namespace ConsoleApplication1
{
public struct PointD
{
public double X;
public double Y;
public double Z;
public PointD(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
public void Set(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
}
public class M
{
static public void Main()
{
List<PointD> ps = new List<PointD>();
ps.Add(new PointD(1, 2, 3));
ps.Add(new PointD(4, 5, 6));
ps[0].Set(7, 8, 9);
Console.WriteLine(ps[0].X);
}
}
}
generic list<> is generating a temporary copy of PointD, and then the Set()
call works on the temporary and so the result is lost. It's just subtle
enough though, that unless you are paying really good attention, it's easy to
miss. FXCop doesn't seem to catch it, not does resharper. Aside from
changing the struct to a class, other ideas? Thanks.
namespace ConsoleApplication1
{
public struct PointD
{
public double X;
public double Y;
public double Z;
public PointD(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
public void Set(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
}
public class M
{
static public void Main()
{
List<PointD> ps = new List<PointD>();
ps.Add(new PointD(1, 2, 3));
ps.Add(new PointD(4, 5, 6));
ps[0].Set(7, 8, 9);
Console.WriteLine(ps[0].X);
}
}
}