I have a class that contains a bunch of properties. It is a mistake by a programmer if they call ToString() on an object of that type. Take this example code:
using System;
public class Foo
{
public int ID = 123;
public string Name = "SomeName";
private string ToString() { return null; }
}
public class MyClass
{
public static void Main()
{
Foo myObj = new Foo();
WL("I want this to be a compiler error: {0}", myObj.ToString());
RL();
}
#region Helper methods
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
#endregion
}
You could reason that if ID is what most people want written out as a string, then I should implement ToString so that it returns the ID. However, I believe that is a bad practice because programmers will "accidentally" get working code. A programmer using my class should specify what they want.
Instead, what I would like is if someone calls myObj.ToString() to have that show up as a compile time error. I thought I could do that by creating a private ToString() function, but that doesn't work.
The reason I brought this up is that we ended up with a query string that contained the fully qualified class name rather then an ID.
So the question is: Is there any way to "hide" the ToString() function so that calling it on an object of my class causes a compiler error?