Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The (relatively) new CallerMemberName attribute is useful, but I would like to use it in some overrides like ToString(). The problem is that when I add it to my method, the signature changes sufficiently so that the override is no longer seen as such.

In Visual Studio 2015, it shows a red squiggly under it indicating:
Item.ToString(string): no suitable method found to override

Which sort of makes sense, since we have added a string argument, but on the other hand it doesn't, because it's an optional parameter. Is there a way to make this work short of creating a MyToString() and changing all references in my code to that?

What I have tried:

C#
class Item
    {
        public override string ToString([CallerMemberName] string caller = null)
        {
            return base.ToString();
        }
    }
Posted
Updated 24-Feb-17 12:25pm

1 solution

It can't be used that way. That is why the error message is there. You can read more about CallerMemberName here: CallerMemberNameAttribute Class (System.Runtime.CompilerServices)[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900