我如何将其重写为lambda? [英] How do I rewrite this as a lambda?
问题描述
我认为这个问题可能很简单,但我是lambdas的新手,并且无法解决以下语法(或者甚至可能)。
是否可以使用 Thickness
构造函数中的内联lambda重写以下代码片段?
double visualUnits = 0.0;
if(Device.RuntimePlatform == Device.iOS)
visualUnits = 20.0;
Padding = new Thickness(10.0,visualUnits,10.0,5.0);
注意:厚度
构造函数需要 double
值和 Device.RuntimePlatform
和 Device.iOS
是 string
值,全部来自Xamarin API。
我尝试过:
我已经尝试了各种各样,但发布它们没有太大意义,因为它们甚至不会编译。
看来,正如已经评论过的那样,我过于复杂了。它根本不需要lambda。以下标准C#语法工作正常:
填充=新厚度(10.0,(Device.RuntimePlatform == Device.iOS)? 20.0:0.0,10.0,5.0);
我知道这个问题可能很简单。 (厚表情符号在哪里?)
然后那应该给你一个线索。我认为从构造函数复杂化中无法获得任何东西。请参阅 lambda表达式 - Google搜索 [ ^ ]。引用:它们甚至不会编译。
I think this question might be stupidly simple, but I'm new to lambdas and just can't work out the syntax for the following (or if it is even possible).
Is is possible to rewrite the following code fragment using an inline lambda in the Thickness
constructor?
double visualUnits = 0.0; if( Device.RuntimePlatform == Device.iOS ) visualUnits = 20.0; Padding = new Thickness( 10.0, visualUnits, 10.0, 5.0 );
Note: the Thickness
constructor is expecting double
values and Device.RuntimePlatform
and Device.iOS
are string
values, all from the Xamarin API.
What I have tried:
I've tried all sorts, but there is not much point in posting them because they won't even compile.
It seems, as has been commented, I was over complicating this. It doesn't require a lambda at all. The following 'standard' C# syntax works fine:
Padding = new Thickness( 10.0, ( Device.RuntimePlatform == Device.iOS ) ? 20.0 : 0.0, 10.0, 5.0 );
I knew the question was probably stupidly simple. (Where's the thicko emoticon?)
Quote:they won't even compile.
Then that should give you a clue. I don't think there is anything to be gained from over complicating a constructor. See lambda expressions - Google Search[^].
这篇关于我如何将其重写为lambda?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!