gpt4 book ai didi

c# - 当用作委托(delegate)参数时,unimplement 部分方法是否会被 null 替换?

转载 作者:行者123 更新时间:2023-12-02 05:12:47 35 4
gpt4 key购买 nike

所以我有两种不同的方法。

一个是普通方法

void DoSomething(delegate x)
{
foreach(......)
{ x(); }
}

另一个是部分但未实现的

partial void DoWorkInForEach();

当我这样调用我的第一个方法时

DoSomething(DoWorkInForEach);

会发生什么,委托(delegate)参数是否为null,我的整个方法调用是否会被删除?

最佳答案

来自language specification :

10.2.7 Partial methods

[...]

If a defining declaration but not an implementing declaration is givenfor a partial method M, the following restrictions apply:

It is a compile-time error to create a delegate to method (§7.6.10.5).

• It isa compile-time error to refer to M inside an anonymous function thatis converted to an expression tree type (§6.5.2).

• Expressionsoccurring as part of an invocation of M do not affect the definiteassignment state (§5.3), which can potentially lead to compile-timeerrors.

• M cannot be the entry point for an application (§3.1).

如果需要,您可以在此处使用 lambda 而不是方法组,这实际上会给您一个空操作委托(delegate):

DoSomething(() => DoWorkInForEach());

关于c# - 当用作委托(delegate)参数时,unimplement 部分方法是否会被 null 替换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15139167/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com