Quote:Original post by SneftelQuote:Original post by -MadHatter
reference types are passed by reference. value types are passed by value. anything declared as a struct is a value type, while things declared class are reference types.
You seem to be confusing reference types with reference passing. C# passes both value and reference types by value, unless the ref keyword is given. The remainder of your post shows that you understand the practical upshot of C#'s system, but you should get your terminology straight. There's nothing "sort of" about C#'s evaluation strategy. ref variables are passed by reference; everything else, value type or reference type, is passed by value.
Think I need to clear something up here before people get confused.
First of all we will go over some terms to make sure we are all on the same page.
reference type and value type - these are the two base types, value types are structs and other base types such as int, float, ect. When you use a value type a copy of the information is made and that is used. A reference type (classes) are used by reference, so when you use them the reference is passed around not the data itself.
reference parameters vs. value parameters. Unless you use the 'ref' keyword, the parameter for a method will be a value parameter, so the parameter will be passed by value.
So what does this mean to us? Well that is where it gets a little strange.
take the fallowing
DoSomething(myClass a);
That is a value parameter, so we are passing it by value but what does it really mean? It means that we are coping the data. So what data gets copied? The reference to a gets copied and passed in. So why is this important? Let me show you.
void DoSomething(myClass a)
{
a = new myClass();
a.Text = "Did something";
}
So we do this.
myClass first = new myClass();
first.Text = "Some text";
Now we call the function.
DoSomething(first);
What will be the value of first.Text when we are done? It will still be "Some text";
Now we call the functions using a function call like so; void DoSomething(ref myClass a);
What will the value of first.Text be when you are done? "Did something".
So why is that? Because in this case we are passing the acutal reference to first and not just a copy. In the first case we changed the reference and so we were no longer referencing 'first', in the second one we were always referencing 'first'.
I really hope that didn't confuse anyone. By value of a reference is just a copy of the reference and doesn't really matter unless you change the reference.
theTroll