C# Error CS0668 – Two indexers have different names; the IndexerName attribute must be used with the same name on every indexer within a type

C# Error

CS0668 – Two indexers have different names; the IndexerName attribute must be used with the same name on every indexer within a type

Reason for the Error & Solution

Two indexers have different names; the IndexerName attribute must be used with the same name on every indexer within a type

The values passed to the IndexerName attribute must be the same for all indexers in a type. For more information on the IndexerName attribute, see .

The following sample generates CS0668:

// CS0668.cs  
using System;  
using System.Runtime.CompilerServices;  
  
class IndexerClass  
{  
   [IndexerName("IName1")]  
   public int this [int index]   // indexer declaration  
   {  
      get  
      {  
         return index;  
      }  
      set  
      {  
      }  
   }  
  
   [IndexerName("IName2")]  
   public int this [string s]    // CS0668, change IName2 to IName1  
   {  
      get  
      {  
         return int.Parse(s);  
      }  
      set  
      {  
      }  
   }  
  
   void Main()  
   {  
   }  
}  

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