Why is the type of the ref parameter different from the regular type?

I understand how to go between two different types, but my question is; why is this a difference in level level ?

I would think that this would be a property of the ParamterInfo object, not a separate special type.

Assuming that it is presented as a separate type in reflection, because that is exactly what it is internally, what language advantages exist to have it as a separate type (I guess an easier method overload resolution or something else)?

At the same time, why does ref do a separate type but out does not (I can't think of the reasons why ref is a separate type that does not apply to out )?

+6
source share
2 answers
 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.

+5
source

ref parameters are of different types, because ref types are also allowed and useful in other contexts than parameters. C # does not allow, but other languages ​​(at least C ++ / CLI) support, for example, local variables of reference type. Such a thing makes sense for ref , but not for out .

Pretending C # allows this, you can write (this is supported by IL):

 int x = 3; ref int y = x; y = 4; if (x == 4) MessageBox.Show("x is 4"); 

This is not an example of when it is useful, but in the same situations where useful ref parameters are useful, it is also useful to use a helper class or structure with a ref field.

+6
source

Source: https://habr.com/ru/post/906189/


All Articles