So I've been told that the compiler translates parameters with a signature in char
to char*
:
Also, if it's an
in
, you should not specify the pointer.in
makes it a reference type.in float
is equivalent toconst float*
.
That got me thinking. Does out char
also translate to char*
? Why does the compiler do this? How does this compare to the usage of [In]
and [Out]
attributes?
I've been told that the compiler translates parameters with a signature
in char
tochar*
Whoever told you that is not explaining the situation well. Perhaps they are attempting to make an analogy to C? C# references are similar to pointers in C, but they are not the same. The better analogy would be to C++ references, but they are not exactly the same either. Rather than trying to understand the feature by analogy to another language, try to understand the feature based on its documentation.
The correct way to think about this is that in
, out
and ref
all require that the argument corresponding to the formal parameter be a variable, and the formal becomes an alias for the variable argument. That is the level of abstraction you should be thinking at.
In practice, in
, out
and ref
are implemented by using managed pointers to the variable. However you should not think about this as char*
. That's the type of an unmanaged pointer to a variable. Managed pointers have many properties that are not properties of unmanaged pointers; for example, they are robust in the face of the garbage collector compacting a region. For example, you can make a managed pointer to a managed type, but you cannot make an unmanaged pointer to a managed type. And so on.
Why does the compiler do this?
Because doing so correctly implements the specification.
How does this compare to the usage of
[In]
and[Out]
attributes?
I don't understand what two things you wish to be compared. The question is unclear.
Use those attributes for COM interop. Read the documentation for those attributes if you are unclear what they are for.