【问题标题】:Partial Methods in C# and parameters [closed]C# 中的部分方法和参数 [关闭]
【发布时间】:2018-01-11 09:30:25
【问题描述】:

Ref 和 out 可以改变函数参数的行为。有时我们希望将变量的实际值复制为参数。其他时候我们需要参考。这些修饰符影响明确的赋值分析。

我的问题是:C# 中的部分方法可以有 ref、out、可选的输入参数吗?

【问题讨论】:

标签: c# ref out partial-methods


【解决方案1】:

通过对this example 中的代码进行实验,您似乎可以发现可以使用refparams 和默认参数值,但不能使用out

partial class A
{
    partial void OnSomethingHappened(string s);
    partial void useRef(ref string s);
    partial void useOpt(string s1, string s2 = null);
    partial void useArgs(params string [] s);
}

// This part can be in a separate file.
partial class A
{
    // Comment out this method and the program
    // will still compile.
    partial void OnSomethingHappened(String s)
    {
        Console.WriteLine("Something happened: {0}", s);
    }
}

另外,正如@user6144226 的the docs linked 解释和@marc_s 指出的那样:

部分方法可以有 ref 但不能有 out 参数。

【讨论】:

  • downvotes 的工具提示说“这个问题没有显示任何研究工作;它不清楚或没有用。”好吧,你有它。
  • 这很公平。
  • Partial 方法不支持 OUT 参数有什么具体原因吗?
猜你喜欢
  • 1970-01-01
  • 2011-10-18
  • 2014-07-26
  • 2012-01-21
  • 1970-01-01
  • 2013-05-09
  • 2018-07-01
  • 2014-02-17
  • 1970-01-01
相关资源
最近更新 更多