How do I use parameters and return in C#?
For the life of me, every tutorial I look through, every video I've seen, every book I've read, I just can not understand how these things work exactly.
I see passing 开发者_运维知识库Values by Value, Value by Reference, Reference by Value, Reference by Reference. Can someone help me understand these with the most basic examples?
Same question in regards to return types. How do they work?
The best resource you will find on this is Parameter passing in C# by Jon Skeet. It gives a very clear explanation. If after reading that you still have questions, you should try to ask them more specifically so it is easier to help you.
First of all , you only see passing by value , or passing by reference. Not anything else. Now to understand this concept, imagine you have a set of digital boxes marked 1,2,3, 4 etc. These boxes contain some data. Then imagine you have a list of paper that states the same thing, i.e an index with 1,2,3,4.
Now you want to pass one of the contents(data) of the boxes to your friend. You can do it in one of these two ways.
Passing by Value Your friend checks your box, and copies it in his box/harddrive whatever. Assume your friend needs to change this data. If he does so, your original box is not affected because he changes the data that is in his box.
Passing by Reference You just give the address of the box to your friend, and your friend will USE YOUR BOX instead of copying it. If he modifies the data, it will be from your box, your original data will be changed.
Before you understand return types, you have to understand data types. Begin there.
Let's back up a bit and start with variables.
Variables always contain some specific information. For value types, the information contained is the entire value directly. For reference types, the information contained is only a reference to some other memory location where the real value lives.
In .Net, you can pretend that variables are only really passed to or returned from functions in one way: by value. Forget passing by reference for now — it will just confuse things. When you pass a variable by value, the entire information directly held by the variable is copied to the function. The trick is understanding what this means for reference types, and that is simply that a copy of the reference is taken.
I can demonstrate with a simple code sample:
void Test(int x, dynamic y)
{
x = 3;
y.a = "four";
y.b = "five";
y = new {a = "six", b = "seven"}; // this will have no effect outside the function
}
int x = 2; // value type
var y = new {a = "one", b="two"}; //reference type
// both x and y are passed by value, but y is a reference type so the reference itself is copied
Test(x, y);
Console.WriteLine(x); //writes 2, because the test function is working with a copy
Console.WriteLine(y.a); //writes four
Console.WriteLine(y.b); //writes five
In the case of reference types, you can force a true "pass by reference", but you should never do this unless you really know what's going on. That would look something like this:
void Test(ref dynamic y)
{
y = new {a = "three", b="four"};
}
var y = new {a = "one", b="two"};
Test(y);
Console.WriteLine(y.a); //prints one
Console.WriteLine(y.b); //prints two
The output is still "one" and "two" because the function replaced the reference for the local variable. The code that called the function still has the reference to the original memory location, and so outputs the original value.
精彩评论