Setting a ref to a member field in C#

前端 未结 6 1918
悲哀的现实
悲哀的现实 2021-02-08 13:32

I\'d like to assign a reference to a member field. But I obviously do not understand this part of C# very well, because I failed :-) So, here\'s my code:

public          


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-08 14:09

    At this line:

    this.parameter = parameter;
    

    ...you copy the method parameter to the class member parameter. Then, in Init() you are assigning the value "success", again, to the class member parameter. In your Console.Writeline, then, you are writing the value of the method parameter, "failed", because you never actually modify the method parameter.

    What you are trying to do - the way you are trying to do it - is not possible, I believe in C#. I wouldn't try passing a string with the ref modifier.

提交回复
热议问题