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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...