• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by maxizrin · Dec 22, 2017 at 04:39 PM · methodhidecompiler erroroverrideoperator

member names cannot be the same as their enclosing type

I'm getting this error: "member names cannot be the same as their enclosing type."
For this bit of code:

 public class Item : Dictionary<string, string> {

 public string this[string str] {
         get { return ContainsKey(str) ? "" : null; }
         set { Add(str, value); }
     }
 }

Oddly, visual studio compiles the project without error.
There are no functions named "Item", which would normally cause this error.


I tried adding a function with the class name, just to be sure that VS does indeed recognize it, and sure enough the function immediately gains a red zigzag line under its name.


I checked the syntax, it seems OK, even ReSharper (it's a really cool plugin) recommends adding "new" as the method hides the base class method.


As of me writing this, I'm using Unity 2017.3.0f3 Personal, 64bit.
Visual Studio Enterprise 2017.
Windows 8.1

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ShadyProductions · Dec 22, 2017 at 08:24 PM

The problem is not actually unity related. If your class is named 'Item' and has an indexer declared as this, you may get this error. A default indexer is given the name 'Item' in the emitted code, creating the conflict. Simply rename your class.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maxizrin · Dec 23, 2017 at 03:01 PM 0
Share

So this is a C# thing? The compiler didn't complain about it, very strange. Anyway, it worked, thank you.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Are inspector values supposed to override [HideInInspector] code? 1 Answer

Can't override GetHashCode (cannot change return type when overriding method int UnityEngine.Object.GetHashCode") 1 Answer

OnTriggerEnter2D(collidertype coll) C# 2 Answers

Why does this run it's "super" method? 1 Answer

InvokeRepeating can't call method 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges