Just want confirmations since this is to be the case...
Printable View
Just want confirmations since this is to be the case...
In C# you have to declare the argument using the "ref" keyword, otherwise parameters will be passed by value. Note that the implications of passing by reference and by value are slightly different for value types and reference types. It is the variable that you are passing by value or by reference. With value types the variable contains the object while with reference types the variable contains a reference to the object.
Why is it tha when I passed a custom collection of mine as a parameter to a method then added values to that collection then the added values appears to the passed collection?
It is the same scenario as I described here...
Um... that's a VB6 question. Why would that have anything to do with C# or .Net? :confused:
As already mentioned by JM, when you pass reference types you are actually passing the reference of the object unlike value types where the type holds the values too. So as collection is a reference type when you pass it to another method and do some changes to it, you are actually changing the same object because both contain the reference to the same object.Quote:
Originally Posted by dee-u
That's exactly why I specified that you are passing the VARIABLE by reference or by value, not the object. When you pass a reference type object by value you are passing a copy of the variable. That means you have two variables that refer to the same object. If you make changes to the object via that reference then that change will be reflected in the original. If you assign a new object to that variable then that change will not be reflected, because the original variable still refers to the original object. If you actually want to make changes to a reference type object past as a parameter without those changes being reflected in the original then you need to create a copy of the actual object and pass a reference to that. Why you would want to do this though I don't know.
Hmmmnnn... Does it mean my business object is also of reference type?
Every class is a reference type and every structure is a value type.
Ahhh... Thanks again! :thumb: