Why can't I seal a single indexer?

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

Nicholas Paldino [.NET/C# MVP]

Ole,

But that's exactly what you did on B! From the "C# Programmers
Reference":

The sealed modifier can be applied to classes, instance methods and
properties. A sealed class cannot be inherited. A sealed method overrides a
method in a base class, but itself cannot be overridden further in any
derived class. When applied to a method or property, the sealed modifier
must always be used with override (C# Reference).

You sealed the overriden indexer, and killed any ability that derived
classes had to override that inherited member.

Hope this helps.
 
T

Tom Spink

Nicholas said:
Ole,

But that's exactly what you did on B! From the "C# Programmers
Reference":

The sealed modifier can be applied to classes, instance methods and
properties. A sealed class cannot be inherited. A sealed method overrides
a method in a base class, but itself cannot be overridden further in any
derived class. When applied to a method or property, the sealed modifier
must always be used with override (C# Reference).

You sealed the overriden indexer, and killed any ability that derived
classes had to override that inherited member.

Hope this helps.

Hi Nicholas,

He sealed the indexer that expects a string parameter, not the indexer that
expects an integer parameter.

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

Guest

Looks like a compiler error to me - or have I overlooked
some obscure statement in the specs?

If the "obscure statement" is in the spec, I couldn't find it either - the
spec is long and complex though so I could have missed it. Perhaps this is
worth opening as a potential bug at the Microsoft product feedback center?

Mark
 
O

Ole Nielsby

I said:
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 said:
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
 
T

Tom Spink

Ole said:
(code example at the end of this post.)

UPDATE: You may be interested to know that the Mono C# Compiler (GMCS) does
not have a problem with this. It compiles correctly, and produces a CLI
compliant assembly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top