Export (0) Print
Expand All
Expand Minimize

Compiler Error CS0197

Cannot pass 'argument' as ref or out, because ' argument ' is a marshal-by-reference class

Any class that derives (directly or indirectly) from System.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 never be a proxy object.

The following sample generates CS0197:

// CS0197a.cs
using System;
class X : 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
      // try the following lines instead
      // M m = new M();
      // m.i = 12;
      // AddSeventeen(ref m.i);
   }
}

The following sample generates CS0197 when you try to pass the structure System.Drawing.Point as a reference:

// CS0197b.cs
public class A : System.MarshalByRefObject
{
   public System.Drawing.Point point;
}

public class B
{
   public void Notify() 
   {
      A a = new A();
      if ( !a.point.IsEmpty )   // CS0197
      // try the following line instead
      // if(!((System.Drawing.Point)((object)(a.point))).IsEmpty)
         {
         }
   }
   static void Main()
   {
   }
}
Show:
© 2014 Microsoft