C
C#2y ago
Reda

Simple newbie problem I need someone to explain to me

See the two screenshots, the first one assigns the changed value by using x = ChangeValue(x); while the second tries to change the value by only calling the method ChangeValue(x); but it doesn't work. I would like to know why? Since it already calls the method that returns the value of x, and it has the argument set up, why does it still not work and it is necessary to assign the method to x ? I hope I explained it clearly! I'm very new.
4 Replies
SinFluxx
SinFluxx2y ago
Once you've passed x into the ChangeValue method it's no longer working on the x variable itself, it's just working with the value of x, it no longer has any connection to x, hence why you have to assign it afterwards like your first example
Reda
RedaOP2y ago
aaah understood
SinFluxx
SinFluxx2y ago
You can pass arguments to a method by reference, which let's them work with the original variable itself https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref
Reda
RedaOP2y ago
thank you so much mate got it

Did you find this page helpful?