0
C# Passing Arguments
Hello. In C#, there are 3 ways of passing arguments. By value, reference, and as out put. What are their differences and when do you usually use them? Thx :D
9 Respuestas
+ 2
hinanawi, no problem. programming is complicated stuff. so complicated that it takes years for a normal person to get it down well. SoloLearn doesn't tell people, 'hey, if you go thru this course in a month and you don't remember 90% of the material, then it's okay because 99% of programmers are the same way!' These SoloLearn lessons try to encourage people to play with programming, which is good. but there is more than one side to this coin. it really is complicated too. and that's okay. complicated is nothing we shud be scared of. but it's okay to take years to learn this stuff well. that's normal.
+ 1
example:
when a function has a parameter which is passed by reference, any modifications to that variable will happen outside the scope of a function
when a function has a parameter with the 'out' keyword, the value becomes write-only within the function(or something along those lines, anyone can correct me on that)
+ 1
@hinanawi, 'out' parameters are *not* write only. they are *meant* to be set in the method that is called. so they are exactly the opposite.
0
i think this code gives a good example of the difference between passing by value and passing by reference:
class Test {
static void Main() {
int i = 10;
PassByRef(ref i);
// Now i is 20
PassByValue(i);
// i is *still* 20
}
static void PassByRef(ref int x)
{ x = 20; }
static void PassByValue(int x)
{ x = 50; }
}
0
so in my example that i just posted, when a parameter is passed by value, then changing the value of the parameter in the method does *not* change the value in the original method because only a *copy* of the value is passed (and changed and then never used again).
but when a parameter is passed by reference then the parameter *is* the real one that is passed. so changing it in a method (variable x) will change its value in the original method (variable i).
0
an 'out' parameter is very, very similar to a ref parameter. but you shud foces on understanding ref parameters first, and value types and reference types too. when you use a reference type that is passed as a value, like an array, then it acts like a parameter that is passed as a reference too. arrays and lists are reference types. so if you pass an array 'by value' then changing it in a called method always changes the array, because arrays are *always* passed by reference, even without the 'ref' keyword.
0
int and decimal and float and double are value types. so these need a 'ref' keyword if you want them to change outside the method that sets their value to a different value.
0
an 'out' parameter is like a 'ref' parameter, except for two things i kan think of:
1: where the variable is 'declared', and
2: all out parameters must be set in a method before execution returns from that method (because they are always meant to pass a value 'out' from the method).
0
Shawn Kovac yes, i got it confused, thanks for correcting