How does the compiler infer types in the delegate example?
c#, delegates
Solution
You declare it in your delegate.
delegate double Doubler(double x);
x is your alpha.
You could easily replace your code with:
Doubler dbl = delegate (double x)
{
return x*2;
};
Also you could simplify your lambda expression:
Doubler dbl = alpha => alpha*2;
Problem
In the following delegate example, how does the compiler infer what type the variable alpha is? ``` delegate double Doubler(double x); public class Test { Doubler dbl = (alpha) => //How does it determine what type is alpha? { return alpha * 2 }; Console.WriteLine(dbl(10)); //Is it when the method is called? int here; Console.WriteLine(dbl(5.5)); //double here??? } ``` I found this statement on a website, I guess based on the responses, is it incorrect? "In our example, we specified the type of the argument. If you want, you can let the compiler figure out the type of argument. In this case, pass only the name of the argument and not its type. Here is an example:"