C# 中等效的 Objective-C 代码块
我如何用 C# 编写等效代码:
typedef void (^MethodBlock)(int);
- (void) fooWithBlock:(MethodBlock)block
{
int a = 5;
block(a);
}
- (void) regularFoo
{
[self fooWithBlock:^(int val)
{
NSLog(@"%d", val);
}];
}
How would I write the equivalent code in C#:
typedef void (^MethodBlock)(int);
- (void) fooWithBlock:(MethodBlock)block
{
int a = 5;
block(a);
}
- (void) regularFoo
{
[self fooWithBlock:^(int val)
{
NSLog(@"%d", val);
}];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的东西:
Action
是一个委托,它需要恰好有一个您指定的类型的参数(在本例中为int
),该参数执行时不会返回任何内容。另请参阅常规 C# 委托参考。对于像这样的简单案例,非常简单。然而,我相信 Objective-C 中的块和 C# 中的委托之间存在一些语义/技术差异,这可能超出了这个问题的范围。
Something like this:
Action<T>
is a delegate that takes exactly one argument of a type you specify (in this case,int
), that executes without returning anything. Also see the general C# delegate reference.For a simple case like this, it's pretty straightforward. However, I believe there are some semantic/technical differences between blocks in Objective-C and delegates in C#, which are probably beyond the scope of this question.