C# Error CS1546 – Property, indexer, or event ‘{0}’ is not supported by the language; try directly calling accessor method ‘{1}’

C# Error

CS1546 – Property, indexer, or event ‘{0}’ is not supported by the language; try directly calling accessor method ‘{1}’

Reason for the Error & Solution

Property, indexer, or event ‘property’ is not supported by the language; try directly calling accessor method ‘accessor’

Your code is consuming an object that has a default indexed property and tried to use the indexed syntax. To resolve this error, call the property’s accessor method. For more information on indexers and properties, see .

The following sample generates CS1546.

Example 1

This code sample consists of a .cpp file, which compiles to a .dll, and a .cs file, which uses that .dll. The following code is for the .dll file and defines a property to be accessed by the code in the .cs file.

// CPP1546.cpp  
// compile with: /clr /LD  
using namespace System;  
public ref class MCPP  
{  
public:  
    property int Prop [int,int]  
    {  
        int get( int i, int b )  
        {  
            return i;  
        }  
    }  
};  

Example 2

This is the C# file.

// CS1546.cs  
// compile with: /r:CPP1546.dll
using System;  
public class Test  
{  
    public static void Main()  
    {  
        int i = 0;  
        MCPP mcpp = new MCPP();  
        i = mcpp.Prop(1,1); // CS1546  
        // Try the following line instead:  
        // i = mcpp.get_Prop(1,1);  
    }  
}  

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...