C# passing arguments | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

C# passing arguments

In lesson 28.1 it was said that: “There are three ways to pass arguments to a method when the method is called: By value, By reference, and as Output. By value copies the argument's value into the method's formal parameter. Here, we can make changes to the parameter within the method without having any effect on the argument.” But in lesson 37.1 we can change the value of ‘balanced’ variable without using ‘ref’ or ‘out’. Can anyone please explain me why this is working now without ‘ref’ or ‘out’?

26th Jul 2021, 5:47 PM
Zoltán - avatar
4 Answers
+ 5
Zoltán The first lesson focuses on the behaviors of input arguments passed into a method. The other is about encapsulation of internal class fields that are accessible by methods of the same class instance. The method could work with values that are passed in as arguments as well as class fields and other class members.
27th Jul 2021, 6:13 AM
David Carroll
David Carroll - avatar
+ 4
Because it's a member variable (or field). It is accessable from the methods without parameters. ref and out affect only the arguments. Fields are accessable from methods already and thus don't need to be passed. Methods can modify the fields.
27th Jul 2021, 3:52 AM
你知道規則,我也是 - avatar
27th Jul 2021, 6:27 AM
Monica Martinez
Monica Martinez - avatar
12th Oct 2022, 8:46 AM
Saboh Saboh
Saboh Saboh - avatar