confused penguin
confused penguin

Reputation: 61

Unfamiliar and strange syntax regarding => in C#

I am attempting to port part of an opensource C# program into java, and have run into one piece of code that makes absolutely no sense, i have been unable to find any explanation of the syntax online, and there is no tooltip or even name of the operator given in MCVS, for a more specific search.

branch is a type "Particle3D" which represents a 3d location and rotation. behavior is a delegate (i have replaced it with a simple abstract class in java) for a void function(Particle3D).

b is, according to visual studios, a temporary Particle3D.

What exactly is going on here? it looks like this is assigning a particle3D to a delegate that represents a function that takes Particle3D as an argument, past that i have no idea what the => operator or the following block of code means, i assume it is overloaded in some way (is this assigning an unnamed function to branch.Behavior?)

branch.Behaviour = b =>
{
    LeavesBehaviour(b);
    BranchingBehaviour(branchingPercent, b, depth + 1);

    // weight behaviour
    if (applyWeightOnBranches)
        b.Direction = new Vector3D(initialDirection.X, initialDirection.Y * LineairScaleTo((double)b.Life / (double)branch.MaxLife, -1f, 1f), initialDirection.Z);// +(2 * (((double)b.Life / (double)maxLife)) - 1);
};

My end goal is to get this working properly in java, the rest of the code all ported without any real issues.

Upvotes: 0

Views: 133

Answers (1)

Rob
Rob

Reputation: 27367

The code can be re-written as follows:

void MyMethod(Particle3D b)
{
    LeavesBehaviour(b);
    BranchingBehaviour(branchingPercent, b, depth + 1);

    // weight behaviour
    if (applyWeightOnBranches)
        b.Direction = new Vector3D(initialDirection.X, initialDirection.Y * LineairScaleTo((double)b.Life / (double)branch.MaxLife, -1f, 1f), initialDirection.Z);// +(2 * (((double)b.Life / (double)maxLife)) - 1);
};

And then..

branch.Behaviour = MyMethod;

Note that your current code defines an anonymous method, however, hopefully this should shed some light on what the syntax is representing

Upvotes: 3

Related Questions