Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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.
Posted
Updated 12-Apr-17 1:43am

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?)
 
Share this answer
 
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[^].
 
Share this answer
 
Comments
Patrick Skelton 12-Apr-17 7:39am    
The constructor was just an example. The need for this particular expression (which simply sets a value based on the run-time platform) is required in several places.

I guess a more general version of the problem is: can I write a lambda expression that compares two strings and returns a different value depending upon the result of the comparison?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900