I'm learning lambdas and delegates in C# (and C# in general really), and I want to use these tools to manipulate member values of a struct. The struct is a Vector3, with x, y and z float values. The values are even to 0.5f in my structs (could be 3.5f, 13.5f etc.) and I want to even them out further to whole numbers (3.0f, 13.0f etc.). What I have so far is:
delegate Vector3 PosDelegate (Vector3 pos);
private void SomeFunction(){
Vector3 position = getPositionFromSomewhere();
PosDelegate evenPos = p => p.x -= 0.5f;
evenPos += p => p.y -= 0.5f;
Vector3 newPosition = evenPos(position);
}
I know the problem is that the delegate parameter definition doesn't match the output of the lambda (Vector3 and float mismatch) and I get the errors:
Cannot implicitly convert type 'float' to 'Vector3'
Cannot convert `lambda expression' to delegate type 'PosDelegate' because some of the return types in the block are not implicitly convertible to the delegate return type
but I'm not sure how to proceed. Explicitly casting doesn't work of course. Changing the lambda to something like
evenPos = p => (p.x, p.y, p.z) = (p.x - 0.5f, p.y -0.5f, 0.0f);
yields the same errors.
Any tips are greatly appreciated.