New course! Every coder should learn Generative AI!
Try a free lesson0
Of course you may use a "char" as the DATATYPE of your indexer but not as the IDENTIFIER of it.
For example:
Correct: this [char c] //used as data type
Wrong: this [int char] //used as identifier
--
Refer to this example below:
this is one way of using character as indexer.
using System;
namespace LearnCSharp
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.abc = new char[] { 'A', 'B', 'C' };
Console.WriteLine(p['B']);
Console.WriteLine(p['C']);
Console.WriteLine(p['A']);
Console.WriteLine(p[0]);
Console.WriteLine(p[1]);
Console.WriteLine(p[2]);
}
private char[] abc = new char[3];
public int this[char a]
{
get {
return Array.IndexOf(abc, a);
}
}
public char this[int i]
{
get {
return abc[i];
}
}
}
}