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 6739815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:31:58+00:00 2026-05-26T11:31:58+00:00

I have an interface IKey which I want to have a method which will

  • 0

I have an interface IKey which I want to have a method which will return the key as a string. We looked at having a method like this:

String GetAsString();

which would return the string representation, but would have liked to be able to declare ToString() again in the interface to force implementers to implement it, but it doesn’t force them to as they have an implementation inherited from Object. This was suggested:

public interface IKey
{
    string ToString(string dummyParameter=null);
}

this forces an implementation of the method in any implementing class, but due to the way that optional parameters work callers do not need to provide a value for this, and you ensure that any calls to the ToString() method on objects which are either cast as the interface IKey or the implementing class will always call the class implementation and not the Object implementation.

In the implementations we can just ignore the dummyParameter and return what we want, safe in the knowledge that calling ToString() will always actually call ToString(null).

Now this feels wrong all over to me, but at the same time it does have something quite nice about it. It is almost exactly the same as having a method GetAsString() as this could only be called on the IKey interface and derived classes except that it looks like the more natural ToString() method that we want to use and that we are able to force the implementing of in the child class.

Having said that the dummy parameter which is not used feels wrong.

So is this horrendous? Or great?

And is this question appropriate for SO or should it be on Programmers?

Examples

public class Key :IKey 
    { 
        public string ToString(string abc = null) 
        { 
            return "100"; 
        } 
    }

Key key = new Key ();
Trace.WriteLine (key.ToString());
Trace.WriteLine (key.ToString(null));
Trace.WriteLine (key.ToString("ac"));
Trace.WriteLine (((object)key).ToString());

output:

100
100
100
Blah.Tests.Key
  • 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-05-26T11:31:58+00:00Added an answer on May 26, 2026 at 11:31 am

    From my point of view such ToString() method in a custom interface slightly messed up things because a custom interface exposed a method with standard and well known name ToString().

    I prefer something more straightforward and obvious like:

    string KeyText { get; }
    

    OR method

    string ConvertKeyToString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

if I have interfaces: public interface ANewThing { IKey Key { get; } }
I am planning to develop a site which will have interface very similar to
I have an interface that I have defined in C++ which now needs to
I have custom control and I have interface this control exposes to it's users.
I have custom control and I have interface this control exposes to it's users.
I have interface IResourcePolicy containing the property Version . I have to implement this
I have: @interface SuperClass : UIViewController <UITableViewDelegate,UITableViewDataSource> And then @interface SubClass : SuperClass This
I have interface defs like below. public interface IProvider { } public interface IProviderList
If I have a method with a parameter that's an interface, whats the fasts
Say I have interface IFoo { /// <summary> /// Comments about Bar method goes

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.