title: C#: Force Compiler error on use of myObj.ToString()
body: <p>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:</p>
  
  <pre><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
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p>So the question is: <strong>Is there any way to "hide" the ToString() function so that calling it on an object of my class causes a compiler error?</strong></p>