.Net lambda expression— where did this parameter come from?

前端 未结 5 759
南旧
南旧 2020-12-06 09:50

I\'m a lambda newbie, so if I\'m missing vital information in my description please tell me. I\'ll keep the example as simple as possible.

I\'m going over someone

5条回答
  •  一向
    一向 (楼主)
    2020-12-06 10:34

    The lambda p => QuantitySaved is an expression of type Expression>. Since the method NotifyPropertyChanged is looking for an expression of , it fits.

    So the compiler is able to infer that p is a ViewModelBase. p didn't "come from" anywhere, it's basically being declared right here. It's a parameter for the lambda. It's going to be filled in when someone uses the property parameter of your method. For example, if you put your lambda into a separate variable called lambda, you could call it with lambda(this) and it would return the QuantitySaved value.

    The reason you can't use this in the lambda is because it's expecting a parameter name, and this isn't a valid name. The point is that you could call it on any instance of ViewModelBase, not just the one that created the lambda.

提交回复
热议问题