I wrote:
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.
(code example at the end of this post.)
Tom Spink <ts****@gmail.comadded:
If you leave out the override in the class 'C' the compiler moans
that the abstract member has not been implemented. When you
override the correct member, the compiler moans that it's sealed.
I spent some time with the C# 1.2 specs and I'm now quite sure
this is an error.
(I use the C# 2.0 compiler but these topics are in the C 1.2 specs.)
In § 1.6.6.3:
Indexers can be overloaded, meaning that a class can declare
multiple indexers as long as the number or types of their
parameters differ.
§ 3.6 says the same, in more words.
Chapter 10 details the differences between methods, properties
and indexers and tells that get/set are treated toghether for a
particular signature, but apart from that, indexers are supposed
to be just like instance methods; nothing indicates you cannot
seal different-signatured indexers independently, and it makes
no sense at all that the sealed modifier should have another
granularity than that of the the override modifier.
So, it's a bug.
Perhaps not a catastrophic one, since I can have it work by
outcommenting the sealed modifier, as shown, and if I
really needed to expose it yet prevent overrides, I could
make it nonvirtual and implement it by a virtual method.
What bothers me most is, I like to use the sealed modifier
for refactoring.
In terms of the example: I found that I had done the same
override in classes C, C1, C2... (not presented here) and
decided to move it up to the B class. Sealing it is a neat way
of ensuring I got rid of all the downtown overrides.
(In the real scenario, the indexer signatures are like:
this[Q arg]
this[Q[] args]
Q being a base class of A, and this[Q arg] is declared in
class B, and is the same as this[Q[] args] with a single-element
array. The B class and its C subclasses only support
single-element indexes and are optimized for this by having
an indexer that allows to bypass the single-element array wrap.
I know the paramarray keyword could be used to get the
same syntax, but this would still imply a wasteful wrapping.)
Here comes the code again (copied from my first post).
--------------------------------------------------------------------
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