Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8141887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:39:30+00:00 2026-06-06T12:39:30+00:00

I have a collection class MyCollection<T> . I have implemented T this[string name] ,

  • 0

I have a collection class MyCollection<T>.

I have implemented T this[string name], which works fine.

I tried to also implement T Item(string name) but of course that gave an error:

The type MyCollection<T> already contains a definition for Item

as it should. However I also want to overload Item by implementing a function T Item(string, bool) but this also throws up the same error.

Why would the compiler think that T Item(string, bool) clashes with the indexer T Item(string)? If it weren’t an indexer but just an ordinary method these two declarations would be two perfectly valid overloads.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:39:32+00:00Added an answer on June 6, 2026 at 12:39 pm

    You can absolutely overload an indexer – but you can’t overload it with a method called Item. To overload the indexer, you just need:

    public T this[string name, bool condition]
    {
        get { ... }
        set { ... }
    }
    

    EDIT: Okay, so it looks like:

    • You can’t have two members of different kinds (one method, one property) with the same name, which is correct according to section 10.3 of the C# 4 spec
    • You can’t have an indexer and a property of name Item, which is slightly surprising as they’re both properties under the hood

    I suspect that you can have indexers alongside properties called Item if you change the generated name of the indexer’s property using DefaultMemberAttribute, but I haven’t tried it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic collection MyCollection<T> that I've made, and everything works fine except
I have a collection similar to: Public Class MyCollection Inherits ObservableCollection(Of MyCollection) Private _Name
I have an object that has a child collection: class Person { public string
Let's say we have a collection of Person objects class Person { public string
I have a collection of entities with an any-association, like this: public class CreatedLog
I have a class which contains a static collection that can be used across
I have the following class: class Item { public string[] field1; public int field2;
I have the following collection contract defined: [CollectionDataContract(Name = Centres)] public class Centres :
I have a collection defined as: public class MyCollection : List<MyItem> { ... }
I have a Collection class which has many coins. I am trying to select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.