C# Error CS1655 – Cannot use fields of ‘{0}’ as a ref or out value because it is a ‘{1}’

C# Error

CS1655 – Cannot use fields of ‘{0}’ as a ref or out value because it is a ‘{1}’

Reason for the Error & Solution

Cannot pass fields of ‘variable’ as a ref or out argument because it is a ‘readonly variable type’

This error occurs if you are attempting to pass a member of a variable, a variable, or a variable to a function as a ref or out argument. Because these variables are considered read-only in these contexts, this is not allowed.

The following sample generates CS1655:

// CS1655.cs  
struct S
{  
   public int i;  
}  
  
class CMain  
{  
  static void f(ref int iref)  
  {  
  }  
  
  public static void Main()  
  {  
     S[] sa = new S[10];  
     foreach(S s in sa)  
     {  
        CMain.f(ref s.i);  // CS1655  
     }  
  }  
}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...