c# - What does it mean to call a function without being bound to an identifier -
i understand lambda expressions in sense of:
delegate int del(int i); static void main() { del mydelegate = x => x * x; int j = mydelegate(2); console.writeline(j); console.readline(); }
it's quite neat, , use again, read this:
static void main() { thread t = new thread(() => print("hello t!")); t.start(); console.readkey(); } static void print(string message) { console.writeline(message); }
i can't quite figure out () =>
means, after googling found out (according wikipedia) using anonymous function undefinded (?) and/or not bound identifier.
now, ask mean? doing stated?
() =>
start of lambda expression doesn't have parameters. it's important understand lambda expression can't exist in vacuum - it's part of conversion either delegate type or expression tree type.
in case, being used argument thread
constructor.
if @ thread constructor, there 4 overloads, 2 of have single parameter. 1 takes threadstart
parameter, other takes parameterizedthreadstart
parameter. these both delegate types, parameterizedthreadstart
has parameter whereas threadstart
doesn't.
so there's implicit conversion parameterless lambda expression threadstart
, not parameterizedthreadstart
, call overload taking threadstart
.
so it's equivalent to:
threadstart tmp = () => print("hello t!"); thread t = new thread(tmp);
to use parameterizedthreadstart
, there 3 options:
// x inferred type object parameterizedthreadstart tmp = x => print("hello t!"); // same, brackets around parameter parameterizedthreadstart tmp = (x) => print("hello t!"); // explicit parameter list parameterizedthreadstart tmp = (object x) => print("hello t!");
(again, these inlined thread
constructor call, , compiler infer constructor intended.)
Comments
Post a Comment