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

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