473,836 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static indexers

Why it's impossible to have a static indexer in C#?
Nov 15 '05 #1
12 24824
My guess is that's because only one indexer is allowed in C#, and it is
referenced with the "this" keyword, you cannot have it static since "this"
is meaningless when no instances are available. More than that, this would
prevent having static and non-static indexer at the same time.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Sergey Klementiev" <sk*********@by te-et.ru> wrote in message
news:OM******** ******@TK2MSFTN GP10.phx.gbl...
Why it's impossible to have a static indexer in C#?


Nov 15 '05 #2
An indexer will access some instance data. Static methods and properties may
not access instance data so there's no point in having one.

--
Bob Powell [MVP]
C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

"Sergey Klementiev" <sk*********@by te-et.ru> wrote in message
news:OM******** ******@TK2MSFTN GP10.phx.gbl...
Why it's impossible to have a static indexer in C#?

Nov 15 '05 #3
Bob Powell [MVP] <bob@_spamkille r_bobpowell.net > wrote:
An indexer will access some instance data. Static methods and properties may
not access instance data so there's no point in having one.


An indexer doesn't conceptually *have* to access some instance data
though, any more than a property does - and you can have static
properties. For instance, Encoding.GetEnc oding(string) could be made
(conceptually) into an indexer so that you could use

Encoding["ASCII"] etc if you wanted.

As far as I can understand it's allowed by the ECMA spec, although
admittedly I haven't been able to fudge up an example. I suspect it's
disallowed due to the syntax being a bit odd and it not being useful
very often.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Indexers are intended as a way to make your instance behave as if it was an
array, and static indexers are outside of that usage.

They could be added, but we'd have to be convinced that their utility was
worth the additional complexity. What do you want to use them for?

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Sergey Klementiev" <sk*********@by te-et.ru> wrote in message
news:OM******** ******@TK2MSFTN GP10.phx.gbl...
Why it's impossible to have a static indexer in C#?

Nov 15 '05 #5
"Eric Gunnerson [MS]" <er****@online. microsoft.com> wrote in message
news:OY******** ******@TK2MSFTN GP11.phx.gbl...
Indexers are intended as a way to make your instance behave as if it was an array, and static indexers are outside of that usage.

They could be added, but we'd have to be convinced that their utility was
worth the additional complexity. What do you want to use them for?


For example for key or index-based instance access. Such helper would be
very useful, imho.

SomeClass o = SomeClass[key, ...];

Currency c = Currency["USD"];
Vehicle v = Vehicle["Ford", "Mustang", "1997"]

WBR,
Sergey Klementiev
MCSD EA
Nov 15 '05 #6

"Eric Gunnerson [MS]" <er****@online. microsoft.com> wrote in message
news:OY******** ******@TK2MSFTN GP11.phx.gbl...
Indexers are intended as a way to make your instance behave as if it was an array, and static indexers are outside of that usage.

They could be added, but we'd have to be convinced that their utility was
worth the additional complexity. What do you want to use them for?

The immediate usage that comes to mind, for me, are static classes somewhat
simliar to lookup tables I used to use in C, a global, static, readonly
array. A type with a static indexer could be considered as a static array, a
piece of memory that simply exists with a set of values in it. I can see it
conceptually. Although the functionality can easily be managed using a
static array or IList property, there is the question of why a static type
cannot appear as an array, after all there are static methods, static
properties, static events, etc etc, a static can look like an instance
object in most other respects, I wonder if there is a reason one shouldn't
be allowed to act as an array, when appropriate.
Mind you, this is an issue of conceptuallity, and I would need a fair bit of
convincing myself that the feature is really useful(or proper, but thats a
whole nother can of worms), I just can't find a reason why the concept
doesn't work. --
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights. "Sergey Klementiev" <sk*********@by te-et.ru> wrote in message
news:OM******** ******@TK2MSFTN GP10.phx.gbl...
Why it's impossible to have a static indexer in C#?


Nov 15 '05 #7
Daniel O'Connell <onyxkirx@--NOSPAM--comcast.net> wrote:
The immediate usage that comes to mind, for me, are static classes somewhat
simliar to lookup tables I used to use in C, a global, static, readonly
array. A type with a static indexer could be considered as a static array, a
piece of memory that simply exists with a set of values in it. I can see it
conceptually. Although the functionality can easily be managed using a
static array or IList property, there is the question of why a static type
cannot appear as an array, after all there are static methods, static
properties, static events, etc etc, a static can look like an instance
object in most other respects, I wonder if there is a reason one shouldn't
be allowed to act as an array, when appropriate.
Mind you, this is an issue of conceptuallity, and I would need a fair bit of
convincing myself that the feature is really useful(or proper, but thats a
whole nother can of worms), I just can't find a reason why the concept
doesn't work.


As a concept I'm sure it does work. I believe what Eric was saying
(correct me if I'm wrong, Eric!) is that the occasionally useful time
isn't worth the extra complexity of language in terms of syntax not
only for declaring it in the first place (static this doesn't sound
good, IMO, even if it's the logical extension) but also accessing the
indexer when it's set up. If no other languages support it, that would
also be a problem - it's much less useful to have a feature which only
C# can access.

I think named indexers are a far more pressing issue, myself.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
I think the feature is more than occasionally useful. However, I don't feel
that there
aren't ways to simply work around, and easily work around, the times where you
might use the feature. However, in my case, I tend to think named indexers have
the
same issue. A named indexer is easy to work around by extending a property of a
given class that is itself indexed.

The largest use for static indexers would be as factories. Simple cached access
to class
instances by a series of parameter values that describe how to find the class,
and if it
doesn't exist, how to create it. The same goal can be accomplished with a
static function
(look at WebRequest.Crea te), that internally stores a Hashtable to hold onto
cached classes.

I'm up in the air on this one. Features are nice, but I'm all about the
performance. If adding
named indexers or static indexers in at the language level is going to be faster
and more
performant that me using a class with a default indexed property or a static
creation function
backed by a Hashtable then give it to me, because I want the speed. In this
case the only
feature I can see actually being faster is the named indexer since it'll get rid
of a property
look-up (that I could have made a public read-only field anyway).
--
Justin Rogers
DigiTec Web Consultants, LLC.

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Daniel O'Connell <onyxkirx@--NOSPAM--comcast.net> wrote:
The immediate usage that comes to mind, for me, are static classes somewhat
simliar to lookup tables I used to use in C, a global, static, readonly
array. A type with a static indexer could be considered as a static array, a
piece of memory that simply exists with a set of values in it. I can see it
conceptually. Although the functionality can easily be managed using a
static array or IList property, there is the question of why a static type
cannot appear as an array, after all there are static methods, static
properties, static events, etc etc, a static can look like an instance
object in most other respects, I wonder if there is a reason one shouldn't
be allowed to act as an array, when appropriate.
Mind you, this is an issue of conceptuallity, and I would need a fair bit of
convincing myself that the feature is really useful(or proper, but thats a
whole nother can of worms), I just can't find a reason why the concept
doesn't work.


As a concept I'm sure it does work. I believe what Eric was saying
(correct me if I'm wrong, Eric!) is that the occasionally useful time
isn't worth the extra complexity of language in terms of syntax not
only for declaring it in the first place (static this doesn't sound
good, IMO, even if it's the logical extension) but also accessing the
indexer when it's set up. If no other languages support it, that would
also be a problem - it's much less useful to have a feature which only
C# can access.

I think named indexers are a far more pressing issue, myself.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #9

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Daniel O'Connell <onyxkirx@--NOSPAM--comcast.net> wrote:
The immediate usage that comes to mind, for me, are static classes somewhat simliar to lookup tables I used to use in C, a global, static, readonly
array. A type with a static indexer could be considered as a static array, a piece of memory that simply exists with a set of values in it. I can see it conceptually. Although the functionality can easily be managed using a
static array or IList property, there is the question of why a static type cannot appear as an array, after all there are static methods, static
properties, static events, etc etc, a static can look like an instance
object in most other respects, I wonder if there is a reason one shouldn't be allowed to act as an array, when appropriate.
Mind you, this is an issue of conceptuallity, and I would need a fair bit of convincing myself that the feature is really useful(or proper, but thats a whole nother can of worms), I just can't find a reason why the concept
doesn't work.
As a concept I'm sure it does work. I believe what Eric was saying
(correct me if I'm wrong, Eric!) is that the occasionally useful time
isn't worth the extra complexity of language in terms of syntax not
only for declaring it in the first place (static this doesn't sound
good, IMO, even if it's the logical extension) but also accessing the
indexer when it's set up. If no other languages support it, that would
also be a problem - it's much less useful to have a feature which only
C# can access.


With this I mostly agree, I was more at odds with the statement that
indexers were out of their intended usage(and thereby the concept) at the
type level. I feel the concept and usage is valid, even if the...I guess you
could say value of the usage may not be. There are a great many features in
the language already that greatly increase complexity for rare usage(like
most operator overloading), but that is irrelevent. I don't mean to imply
there is a need for the feature, simply that I think it works from an
expectation standpoint.
I think named indexers are a far more pressing issue, myself.
On this I agree, named indexers(or prarameterized properties) are useful and
would possibly be a good addition to the language, although I fear they will
be abused and used in situations where a method is more appropriate.
However, named indexers starts to give you a decent argument for static
indexers. It would allow you to write static accessor code without having to
write an extra class or relying on dictionaries or lists. Of course, these
indexers being just parameterized properties mitigates that, It would be
confusing however if they were called named indexers and permitted to be
static while unnamed indexers are not. That is more of a naming issue than
anything else.

Btw, while I agree that static this seems wrong, I would think it would be
more appropriate to be...

public object static[string key]
{
get{}
set{}
}
than to use static this. It just seems far clearer to me than any other
possibility, even if it adds another usage for static.
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #10

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

Similar topics

3
2315
by: DKode | last post by:
Ok, Consider the following example: School Class - StudentCollection Property StudentCollection Class : CollectionBase - Add - Item
5
24233
by: TruongLapVi | last post by:
Hi, Why C# does not support Interface static member ? Some time I want implement NullObject Pattern: public interface INullObject { public static INullObject Null { get { return NullObject.Instance; } // !!! Wrong, C# not support ? }
5
3770
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;
1
7576
by: mdub317 | last post by:
I'm totally new to programming and I am wondering; when would be a good time to use an array or an indexer? I want to know what types of applications would make good use of arrays or indexers. There seems to be other ways of doing the jobs of the two and less confusing. The books I read don't provide good examples of situations when I would need an array or indexer. I don't really need a definition of them as I already have that. I...
5
1935
by: bonk | last post by:
Hello, IL does not have indexers. Infact the c# compiler compiles indexers to Set_Item and Get_Item (or whatever name I choose via the IndexerNameAttribute ). So how does c# (compiler) know that something in a IL Assembly is supposed to be an indexer ? And more important, what ways do I have to influence that ?
14
7098
by: knocte | last post by:
Hello. I have a problem with C# language. I want to define an algorithm on a static function inside an abstract class. This function calls a static variable inside the same class. Then I want to define some derived classes wich inherit the function and override the variable, but it doesn't work. Testcase:
8
7515
by: Bill Cohagan | last post by:
I'm curious as to why C# doesn't support static indexers. Anybody know? Thanks, Bill
3
1674
by: Benssol | last post by:
Hi all great programmers and great coders Please can anyone explain clearly the following: usage of indexers? is it used widely (in most applications)? is there is another way that do its function? thanks for all
8
3421
by: dtarczynski | last post by:
Hello all. I have to implement IEnumerator interface in my (static) class. But compilers throws me an error: 'GetEnumerator': cannot declare instance members in a static class For example: private static List<Product_productsList; public static List<ProductProductsList { get
0
9816
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10254
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9371
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7790
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4448
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 we have to send another system
2
4013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.