C
C#16mo ago
Spesceptic

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
SinFluxx16mo 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
Spesceptic
SpescepticOP16mo ago
aaah understood
SinFluxx
SinFluxx16mo 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
Spesceptic
SpescepticOP16mo ago
thank you so much mate got it
Want results from more Discord servers?
Add your server