Change to method declaration

前端 未结 2 1716
再見小時候
再見小時候 2021-01-15 21:43

I was doing an assessment and this is one of the questions I got:

Which of the following changes cannot be made to the declaration of the C# method call(docume

相关标签:
2条回答
  • 2021-01-15 22:07

    As the last one is causing confusion, this might help.

    public class Document
    {
        public void SaveAs(ref string DocName)
        {
    
        }
    }
    

    Notice the parameter name is DocName. Usually this parameter name is thought of as something only used by the method and unimportant outside the method, but since .NET 4 (I think?), C# can used named parameters in this format. If you're familiar with Objective-C then you'll see these often. With named parameters, DocName is important.

    We can now call this method like this

    string fName = "Test.docx";
    Document d = new Document();
    d.SaveAs(DocName: ref fName);
    

    Note that DocName has to be used, otherwise the compiler will thrown an error (so you couldn't do d.SaveAs(RandomName: ref fName);). Also note that a string variable is passed and not instantiated within the method declaration (d.SaveAs(DocName: "Test.docx");).

    0 讨论(0)
  • 2021-01-15 22:15
    • Replace object missing = Missing.Value; with object Missing Remove : will give an error, use of unassigned local variable

    • object fileName = "Test.docx" : : will give an error, use of unassigned local variable

    • statement Remove all occurences of ref : this is fine, will turn the referenced variables into local ones

    The last two i have no idea about, horribly worded.

    0 讨论(0)
提交回复
热议问题