- Home /
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
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.
So this is a C# thing? The compiler didn't complain about it, very strange. Anyway, it worked, thank you.
Your answer
