Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Passing 'argument' as ref or out or taking its address may cause a runtime exception because it is a field of a marshal-by-reference class
Any class that derives, directly or indirectly, from MarshalByRefObject is a marshal-by-reference class. Such a class can be marshaled by reference across process and machine boundaries. Thus, instances of this class could be proxies to remote objects. You cannot pass a field of a proxy object as ref or out. So, you cannot pass fields of such a class as ref or out, unless the instance is this, which can not be a proxy object.
Example
The following sample generates CS0197.
// CS0197.cs
// compile with: /W:1
class X : System.MarshalByRefObject
{
public int i;
}
class M
{
public int i;
static void AddSeventeen(ref int i)
{
i += 17;
}
static void Main()
{
X x = new X();
x.i = 12;
AddSeventeen(ref x.i); // CS0197
// OK
M m = new M();
m.i = 12;
AddSeventeen(ref m.i);
}
}