O
Ole Nielsby
I'm having a strange problem with sealing virtual indexers.
Looks like a compiler error to me - or have I overlooked
some obscure statement in the specs?
I have two virtual indexers in the same class, one of them
by string, the other by int.
The weird thing is, if I seal one of them, I can't override the
other. Here is the code.
--------------------------------------------------------------------
namespace FreakShow
{
class Program
{
static void Main(string[] args)
{
C c = new C();
System.Diagnostics.Debug.Print(c["test"]);
System.Diagnostics.Debug.Print(c[123]);
}
}
abstract class A
{
public abstract string this[int arg] { get; }
public abstract string this[string arg] { get; }
}
abstract class B : A
{
public /*sealed*/ override string this[string arg]
{
get { return "B:" + arg.ToUpper(); }
}
}
class C : B
{
public override string this[int arg]
{
get { return "C:" + (arg * 2); }
}
}
}
--------------------------------------------------------------------
As expected, the debug output shows:
B:TEST
C:246
Now, if I remove the comment marks around sealed, I get this
compiler error:
error CS0239: 'FreakShow.C.this[int].get':
cannot override inherited member 'FreakShow.A.this[int].get'
because it is sealed
I don't think I sealed it??? What's going on?
I'm using C# 2.0.
Looks like a compiler error to me - or have I overlooked
some obscure statement in the specs?
I have two virtual indexers in the same class, one of them
by string, the other by int.
The weird thing is, if I seal one of them, I can't override the
other. Here is the code.
--------------------------------------------------------------------
namespace FreakShow
{
class Program
{
static void Main(string[] args)
{
C c = new C();
System.Diagnostics.Debug.Print(c["test"]);
System.Diagnostics.Debug.Print(c[123]);
}
}
abstract class A
{
public abstract string this[int arg] { get; }
public abstract string this[string arg] { get; }
}
abstract class B : A
{
public /*sealed*/ override string this[string arg]
{
get { return "B:" + arg.ToUpper(); }
}
}
class C : B
{
public override string this[int arg]
{
get { return "C:" + (arg * 2); }
}
}
}
--------------------------------------------------------------------
As expected, the debug output shows:
B:TEST
C:246
Now, if I remove the comment marks around sealed, I get this
compiler error:
error CS0239: 'FreakShow.C.this[int].get':
cannot override inherited member 'FreakShow.A.this[int].get'
because it is sealed
I don't think I sealed it??? What's going on?
I'm using C# 2.0.