I think I don't understand what Type.IsByRef
property from .NET is supposed to indicate. I thought it's supposed to return true for reference types and false for value types, so the opposite of Type.IsValueType
property. I can't get it to return true for types that are obviously reference types though. Here's an example:
using System.Text;
public class Program
{
static void Main(string[] args)
{
try
{
int i = 0;
Console.WriteLine(i.GetType().IsByRef); // returns false - OK
Exception e = new Exception();
Console.WriteLine(e.GetType().IsByRef); // returns false - ??
StringBuilder sb = new StringBuilder();
Console.WriteLine(sb.GetType().IsByRef); // returns false - ??
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadKey(true);
}
}
What am I missing here?
IsByRef
is true for parameters passed by reference:
public void Foo(ref int x) { }
...
var fooMethod = this.GetType().GetMethod("Foo");
var param = fooMethod.GetParameters()[0];
bool isByRef = param.ParameterType.IsByRef; // true
It's unrelated to value types and reference types. To check if a type is a value type, check IsValueType
(which returns true for a value type, false otherwise).