Converting ObjC Blocks to C# Lambas

Posted by Sam on Stack Overflow See other posts from Stack Overflow or by Sam
Published on 2014-06-01T02:39:10Z Indexed on 2014/06/01 3:26 UTC
Read the original article Hit count: 211

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about objective-c