C# passing arguments | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

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
ZoltƔn - avatar
4 Respostas
+ 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
0
ŲµŲ§Ł†ŁƒŲ§ŁŠŁ„Ų§ŲŖŁ†Ų±Ł„Ų³
12th Oct 2022, 8:46 AM
Saboh Saboh
Saboh Saboh - avatar