Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>ParameterInfo.ParameterType.IsByRef returns true if the declaration of the parameter is with the ByRef keyword, and returns false if the declaration is with the ByVal keyword (regardless of whether the type of the parameter is by-value (e.g., structure) or by-reference (e.g., class)).</p> <p>To illustrate, consider the following structure and class (I'm using VB code):</p> <pre><code>' Empty structure and class, just for illustration. Public Structure MyStruct End Structure Public Class MyClass1 End Class </code></pre> <p>And suppose you have the following method that takes ByVal and ByRef arguments for the structure and class defined above (note that starting with VB 2012, you can omit the ByVal keyword as it is the default):</p> <pre><code>Public Sub P(s1 As MyStruct, ByRef s2 As MyStruct, c1 As MyClass1, ByRef c2 As MyClass1) End Sub </code></pre> <p>Now the following code tests the ParameterInfo.ParameterType.IsByRef method:</p> <pre><code>Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click ' Reflect on method P: Dim mi As MethodInfo = Me.GetType.GetMethod("P") ' Iterate all parameters, and call its ParameterType.IsByRef method: For Each pi As ParameterInfo In mi.GetParameters If **pi.ParameterType.IsByRef** _ Then Console.WriteLine(pi.Name &amp; " is ByRef") _ Else Console.WriteLine(pi.Name &amp; " is ByVal") Next End Sub </code></pre> <p>You'll get the following output:</p> <pre><code>s1 is ByVal s2 is ByRef c1 is ByVal c2 is ByRef </code></pre> <p>As you can see, ParameterInfo.ParameterType.IsByRef returns true for arguments s2 and c2 because they are defined with the ByRef keyword, even though one of them is a structure (value-type) and the other is a class (reference-type); and returns false for the arguments defined with the ByVal keyword.</p> <p>Note, however, that the ByVal keyword does not mean that all arguments would be passed as a copy. Even if this keyword (ByVal) is used, if the type is by-reference (e.g., class), the argument would be passed by reference, as if the ByRef keyword were used. That is, c1 and c2 of method P above will BOTH be passed by reference, which means that if P changes a field or property to c1 or c2, the changes will be reflected to the caller. (ByVal and ByRef make difference mostly when the type is a value, such as a structure.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload