MeTitus
MeTitus

Reputation: 3428

Change to method declaration

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(document.SaveAs(...)) below to streamline the code:

object fileName = "Test.docx";
object missing = Missing.Value;

document.SaveAs(ref fileName,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing,
                ref missing);

I already did the assessment, I'm just curious because I actually did not get the question.

Thanks.

enter image description here

UPDATE

I've receive the results and "unfortunately" got just 89% which according to the recruiter, is not enough... anyway, like I said before I choose options D and E, and got the question partially right, and given that I can only select 3 options at the most, options A, B, C and D can't be all correct.

Upvotes: 1

Views: 263

Answers (2)

keyboardP
keyboardP

Reputation: 69372

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");).

Upvotes: 1

iambeanie
iambeanie

Reputation: 315

  • 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.

Upvotes: 0

Related Questions