Attribute <Out ()>. What useful purpose does it serve?
In the System.Runtime.InteropServices attribute <Out()> present. But what is it for? I would be happy if you could use the following example as the basis for your answers.
Shared Sub Add(ByVal x As Integer, ByVal y As Integer, <Out()> ByRef Result As Integer) Result = x + y End Sub The purpose of this attribute is twofold:
- Call handling, whether forcibly initializing variables or not
- Ranging
If you were to call this method from C # or a similar language with similar semantics, such a parameter will be known to the compiler so that the initial value is not required.
In other words, you can do this:
int a; CallSomeMethodWithOutParameter(out a); and the compiler knows that there is no need to guarantee that a already matters before making the call.
On the other hand, without an attribute, the following will be required: C #:
int a = 0; // <-- notice initialization here CallSomeMethodWithOutParameter(ref a); // <-- and ref here Another goal is method calls that will be redirected to another call context, for example, through P / Invoke, to another application domain or web service to notify the marshalling procedures that this parameter will contain a value when the method returns, but when called it does not need to pass any value.
This can make a difference when the parameters and return values need to be packed and transferred to the remote location where the actual call is being made.
In other words, if you pointed out that when you call a method used through P / Invoke, when you call the method, the existing parameter value will not be sorted, but when the method returns its value, it will return your call code.
Please note that this optimization depends on the marshalling procedure used or not, these are implementation details. The attribute simply tells the program what parameters it can work with, this is not an instruction that will always be executed.
This means that the parameter is considered as the "out" parameter with C #, on the one hand. In this case, the C # compiler assumes that:
- Any existing value of a variable passed by reference does not matter, so a specific assignment does not matter.
- The variable will be assigned the appropriate value at the time the method returns, unless an exception exists - therefore, it is definitely assigned at the end of the statement.
Other languages can use the [Out] attribute in many ways, of course, but such an interpretation is the most natural. This basically means that the parameter is almost like an additional return value. (Of course, there are many differences of varying degrees of subtlety, but this is a general feeling of the output parameter.)
It is used in ComVisible types to indicate that the created COM-type library should decorate the parameter with the [out] attribute.
I don't know about VB, but considering it equivalent to the C # out keyword:
It behaves exactly like ref , but does not require the caller to initialize the variable passed to the out parameter because the function will not read it.
And that probably affects marshaling if you use the p-invoke COM command.
When applied to method parameters and return values, these attributes control the direction of the marshaling, so it is known as direction attributes. [OutAttribute] tells the CLR to return it from the caller upon return. Both the caller and the callee can be either unmanaged or managed code. For example, when calling P / Invoke, managed code calls unmanaged code. However, in reverse P / Invoke, unmanaged code could invoke managed code through a function pointer.
There are times when [OutAttribute] ignored. For example, [OutAttribute]int does not make any sense, so [OutAttribute] simply ignored by the CLR. The same is true for the [OutAttribute] , because the line is immutable.
So, for your example, this attribute does not make sense. You can find more about this attribute and the correlated attribute <In()> here .