Converting ObjC Blocks to C# Lambas
- by Sam
I need some help converting an Objective-C block to C#.
Here is the source ObjC:
NSDate* addYear = [_calendar dateByAddingComponents:((^{
NSDateComponents* components = [NSDateComponents new];
components.month = 12;
return components;
})()) toDate:now options:0];
Now I tried the following in C#:
NSDate date = _calendar.DateByAddingComponents((() => {
NSDateComponents components = new NSDateComponents();
components.Month = 12;
return components;
})(), now, NSCalendarOptions.None);
To which I get the following compiler error: Expression denotes a 'anonymous method' where a 'method group' was expected.
Removing the parentheses around the lambda yields Cannot convert 'lambda expression' to non-delegate type 'MonoTouch.Foundation.NSDateComponents'.
What is the correct C# syntax? I need to retain the closures as there are a lot more in the code base that I am porting.