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

  • SEARCH
  • Home
  • 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 8981815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:25:44+00:00 2026-06-15T20:25:44+00:00

I have defined a partial class with a property like this: public partial class

  • 0

I have defined a partial class with a property like this:

public partial class Item{    
    public string this[string key]
    {
        get
        {
            if (Fields == null) return null;
            if (!Fields.ContainsKey(key))
            {
                var prop = GetType().GetProperty(key);

                if (prop == null) return null;

                return prop.GetValue(this, null) as string;
            }

            object value = Fields[key];

            return value as string;
        }
        set
        {
            var property = GetType().GetProperty(key);
            if (property == null)
            {
                Fields[key] = value;
            }
            else
            {
                property.SetValue(this, value, null);
            }
        }
    }
}

So that i can do:

 myItem["key"];

and get the content of the Fields dictionary. But when i build i get:

“member names cannot be the same as their enclosing type”

Why?

  • 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-15T20:25:45+00:00Added an answer on June 15, 2026 at 8:25 pm

    Indexers automatically have a default name of Item – which is the name of your containing class. As far as the CLR is concerned, an indexer is just a property with parameters, and you can’t declare a property, method etc with the same name as the containing class.

    One option is to rename your class so it’s not called Item. Another would be to change the name of the “property” used for the indexer, via [IndexerNameAttribute].

    Shorter example of brokenness:

    class Item
    {
        public int this[int x] { get { return 0; } }
    }
    

    Fixed by change of name:

    class Wibble
    {
        public int this[int x] { get { return 0; } }
    }
    

    Or by attribute:

    using System.Runtime.CompilerServices;
    
    class Item
    {
        [IndexerName("Bob")]
        public int this[int x] { get { return 0; } }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this user defined function. public partial class UserDefinedFunctions { static int i;
Suppose I have an entity object defined as public partial class Article { public
I have defined a ConfigurationProperty class, which is basicly a key-value pair (with key
I have defined a rectangle drawable like this: <shape xmlns:android=http://schemas.android.com/apk/res/android android:shape=rectangle> <corners android:radius=4dip/> <solid
Consider this abstract class public abstract class Foo { public Injectable Prop {get;set;} }
I have used EF to generate classes for my database tables: public partial class
I have defined the following select list: <%= this.Select(Scope) .Options(Scopes.AllValues) // static property to
Let's say I have a class with a public property used for property injection,
Assuming we have such control: public partial class MyUserControl : UserControl { public MyUserControl()
In my SharePoint 2010 c# / asp.net site, I have a class defined like

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.