-6

Ref and out can change the behavior of function parameters. Sometimes we want the actual value of a variable to be copied as the parameter. Other times we want a reference. These modifiers affect definite assignment analysis.

My question is: can partial methods in C# have ref, out, optional input parameters?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tony
  • 1,177
  • 6
  • 18
  • 31

1 Answers1

1

By experimentation with the code in this example it seems like you can find that it is possible to use ref, params, and default argument values, but not 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);
    }
}

Also, as explained by the docs linked by @user6144226 and pointed out by @marc_s:

Partial methods can have ref but not out parameters.

Felipe
  • 10,606
  • 5
  • 40
  • 57