I have problems trying to replicate the inline functions that are widely used in Java
These are not inline functions, that's anonymous classes implementing a specific interface.
C# provides delegates that you can define inline or in a separate function.
Here is an example of sorting a List<DATA>
in place using the Comparison<T>
delegate:
List<DATA> queue = new List<DATA>();
queue.Sort(
(left, right) => {
double distance1 = distanceFunction.Calculate(left, promoted.first);
double distance2 = distanceFunction.Calculate(right, promoted.first);
return Double.Compare(distance1, distance2);
}
);
Note that in order for this to work, the distanceFunction
variable needs to be in scope at the spot where you invoke queue.Sort
. It can be a local variable defined above the invocation point, or a member variable/property of the class enclosing the function that makes the call.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…