473,503 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why can't I seal a single indexer?

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.
Jul 7 '06 #1
6 1931
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ole Nielsby" <ol*********@snailmail.dkwrote in message
news:uF**************@TK2MSFTNGP05.phx.gbl...
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.


Jul 7 '06 #2
Nicholas Paldino [.NET/C# MVP] wrote:
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.

--
Hope this helps,
Tom Spink
Jul 7 '06 #3
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
Jul 7 '06 #4
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
Jul 7 '06 #5
Ole Nielsby wrote:
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.
<snippedy-doo-dah>

I have to agree, it is bizarre.

--
Hope this helps,
Tom Spink
Jul 7 '06 #6
Ole Nielsby wrote:
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.)
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.

--
Hope this helps,
Tom Spink
Jul 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2828
by: Dotnetjunky | last post by:
Hi all Is it correct that there's a link between indexer and Items property in C# ? If yes, please show me what is it ? thanks in advance. -- Dotnetjunky
1
1632
by: Iulian Ionescu | last post by:
I have the following problem. I derived a class from ArrayList to provide some additional events I need. Then I derived 2 different classes from this one and each of them defines an indexer that...
5
3749
by: Michel Walsh | last post by:
Hi, Looking for the syntax for a static indexer. For a non static 'access', the following would do: public class whatever { static Hashtable myHashtable = null;
3
4111
by: Sam Martin | last post by:
Basically, i've got a class called RegularExpressions which contains a private Hashtable of RegExps used in my apps. I obviously don't want to give full public access to the Hashtable, so I'd...
7
4607
by: Steph | last post by:
I'm learning C#. I need to implement an indexer using an array list. I have the following code and I'm getting an error "Inconsistent accessibility: indexer return type CRegs is less accessible...
5
17653
by: rwoo_98 | last post by:
I am trying to pass a reference to a method --SomeMethod. This table resides in a dataset and I refer to the datatable in the dataset using an indexer. However when I attempt to do this I get the...
17
2057
by: SemSem | last post by:
i want to know waht is an index and how we use it with a simple example including the main of the program . thanx -- Islam Khalil,
32
9399
by: Michael C | last post by:
I want to inherit from Bitmap to add a property but I can't because it's sealed. Is there any reason to seal a class? Thanks, Michael
0
998
by: raja | last post by:
Fault Seal Analysis Analyse Reservoir Fault, Lateral & Top Seal quickly and efficiently http://finance4u.synthasite.com/
0
7192
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.