Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C#3.0 C#4.0
There are two important restrictions to using indexers. First,because an indexer does
not define a storage location, a value produced by an indexer cannot be passed as a ref or
out parameter to a method. Second, an indexer must be an instance member of its class; it
cannot be declared static.
1. why we can't pass produced value by indexer as ref or out parameter to an method?
2. why indexers can not be static?
Posted 2-Oct-12 20:37pm
Edited 2-Oct-12 20:41pm
Mehdi Gholam259.4K

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your quote answers your first question pretty exactly:
1) "because an indexer does not define a storage location"
It doesn't produce any storage related value so you can't pint a reference at it. If it doesn't have a value that a reference can point at, it can't be a reference, so it can't be passed as a reference. An Indexer is not a number: it is a process in a stage of execution.
2) Because it is an indexer into a specific instance of the class - if it was static, it would be a global indexer for all instances, and that would be silly - if you have two lists which of them should a global indexer return a value from? What if they have a different number of elements? Indexers have to be related to the specific indexable instance, or they can't work!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Zoltán Zörgő 185
1 Sergey Alexandrovich Kryukov 160
2 Peter Leow 100
3 CPallini 70
4 Ramza360 51
0 Zoltán Zörgő 80
1 the other paul 23
2 Shmuel Zang 10
3 Aarti Meswania 5
4 Member 11389991 -2

Advertise | Privacy | Mobile
Web01 | 2.8.150129.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100