-2

According to MCSD CERTIFICATION TOOLKIT (EXAM 70-483) book 3th chapter test question 15 How are values passed in generic methods? I found answer:

They are passed by reference

But according to my understanding this is not truth. I can have method with generic parameters:

    public static T aaa<T>(T a)
    {
        return a ;
    }

And call it by value:

int i=5;
aaa<int>(i);

Where my understanding is wrong?

UPD

Original question/answer

 15 . How are the values passed in generic methods?
 a . They are passed by value.
 b . They are passed by reference.
 c . They must be encapsulated in a property.
 d . They are passed during class instantiation
vico
  • 17,051
  • 45
  • 159
  • 315
  • Can you add more context? "They are passed by reference" where was this statement made, is there any example in the book? – Selman Genç Sep 28 '18 at 15:20
  • The book might not mean "Value-Types" (e.g. `int`) when talking about "values", but more in the general sense of the word. For example, if you pass a `string` "value", it is "passed by reference" (in a sense; frankly the wording, without more context, is to vague). – Christian.K Sep 28 '18 at 15:23
  • 2
    @Christian.K No, strings are not passed by reference, unless you use the `ref` or `out` keywords. The value that is being copied contains a reference in that case, but that's radically different than saying that the parameter is passed by reference. – Servy Sep 28 '18 at 15:24
  • @Servy "value that is being copied contains a reference in that case" - yes you're right. That's why I said "the wording is too vague without more context". But yes, as it stands that statement was not correct. – Christian.K Sep 28 '18 at 15:28
  • @Christian.K Both your wording, and the wording of the question, isn't vague. It has a very well defined meaning, and that meaning is simply wrong in both of the usages. – Servy Sep 28 '18 at 15:30

1 Answers1

0

Exactly the same way they are passed to non-generic methods.

"the correct answer in the book is B"

Not really. The correct answer depends on the use or not of 'ref'/out'. The question is either ill-formed or taken out of context since the general answer is simply "it depends": if ref/out are used when the function argument is passed by reference otherwise it is passed by value.

There's certain confusion around "by reference" and "reference types". This is easy to sort out if you think about what exactly is the argument in the case of reference types and in the case of value types:

for reference types, the argument value is a reference to the object for value types, the argument value is the object itself If you combine value/reference types with pass by value/reference you get 4 cases:

value type passed by value - a copy of the object is passed to the method value type passed by reference - a reference to the object is passed to the method reference type passed by value - a reference to the object is passed to the method reference type passed by reference - a reference to an object reference is passed to the method