public static int SomeMethod(string local, ref string strParam) { local = SomeStaticlyHeldString; strParam = SomeStaticlyHeldString; int localInt = local.Length; return strParam.Length; }
Assigning local means that the memory location that the local label now points to is the same object that SomeStaticlyHeldString was SomeStaticlyHeldString .
The strParam means that the memory space passed as an argument to the method using ref labels now points to the same object that SomeStaticlyHeldString was SomeStaticlyHeldString .
Getting local.Length requests the object that local points to. Getting strParam.Length requests the object that the variable pointed to by strParam points to, points to.
Both really behave differently, not only at a point defining a parameter or local, but each time they are used. The fact that the difference is largely hidden makes them more and more different, since each operation on them is essentially different.
If we had a lower-level language that had nothing but local variables, objects on some non-local heap and pointers to both of them, then local would be of type string* and strParam of type string** . That would be how we would do a similar operation in C and how we could do it in C ++, although it also has reference types (although with C ++, the type of the type of the reference type is a clearer part of determining its type and they have further use and refinement). C # hides almost all of this from us in its syntax. It is always debatable how any concealment of details is useful, but in this case there is not much in the way of anything useful hiding, so one that one will be hard pressed to criticize.
source share