0

I came across a code, which is bit confusing for me. In the below code, isVisible is not declared, still it is able to assign value to the delegate.

Action<bool> onChangeLoader = (isVisible) => { };
Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
Shriniketh
  • 21
  • 3
  • You've created an anonymous method (return type: void) which accepts a bool and does nothing. You've then stored that method as a delegate in `onChangeLoader`. `isVisible` is a bool as inferred from `Action`. – ProgrammingLlama Oct 14 '22 at 07:21
  • 1) [Delegates in C#](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/) 2) [Lambdas in C#](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions) 3) [Delegates with Named vs Anonymous Methods](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/delegates-with-named-vs-anonymous-methods) – ProgrammingLlama Oct 14 '22 at 07:22
  • Action onChangeLoader means is that onChangeLoader is a method pointer and this method has a parameter in type bool. ( isVisible ) => this is an anonim method decleration. isVisible type come from left part of equal – Ramin Quliyev Oct 14 '22 at 07:26

1 Answers1

1

That code is equivalent to having this method:

private void DoSomething(bool isVisible)
{

}

and then assigning a delegate for that method to a variable:

Action<bool> onChangeLoader = DoSomething;

After that, anywhere that you might call the DoSomething method, you could invoke the onChangeLoader delegate.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46