C#中的部分方法和参数
问题描述:
Ref和Out可以更改函数参数的行为。有时我们希望将一个变量的实际值作为参数进行复制。其他时候我们想要参考。这些修饰符影响明确的赋值分析。C#中的部分方法和参数
我的问题是:可以在C#中的部分方法有ref,out,可选的输入参数?
答
通过与代码实验中this example好像你可以发现,这是可以使用ref
,params
,并且默认参数值,但不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);
}
}
此外,由@由the docs linked解释user6144226并由@marc_s指出:
部分方法可以有ref但不能输出参数。
你试过了吗? :) – Felipe
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods#partial-methods – user6144226
从上面的链接文档:*部分方法可以参考,但不能输出参数。* –