Created
September 19, 2012 20:51
-
-
Save orangutanboy/3752178 to your computer and use it in GitHub Desktop.
ToString interface overrides
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Demo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var myClass = new MyClass(); | |
Console.WriteLine("Class: " + myClass.ToString()); | |
Console.WriteLine("Interface: " + ((MyInterface)myClass).ToString()); | |
Console.WriteLine("object: " + ((object)myClass).ToString()); | |
Console.ReadLine(); | |
} | |
} | |
public interface MyInterface | |
{ | |
string ToString(); | |
} | |
public class MyClass : MyInterface | |
{ | |
public override string ToString() | |
{ | |
return "override"; | |
} | |
string MyInterface.ToString() | |
{ | |
return "explicit"; | |
} | |
} | |
} | |
/* | |
displays | |
Class: override | |
Interface: explicit | |
object: override | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Demo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var myClass = new MyClass(); | |
Console.WriteLine("Class: " + myClass.ToString()); | |
Console.WriteLine("Interface: " + ((MyInterface)myClass).ToString()); | |
Console.WriteLine("object: " + ((object)myClass).ToString()); | |
Console.ReadLine(); | |
} | |
} | |
public interface MyInterface | |
{ | |
string ToString(); | |
} | |
public class MyClass : MyInterface | |
{ | |
string MyInterface.ToString() | |
{ | |
return "explicit"; | |
} | |
} | |
} | |
/* | |
displays | |
Class: Demo.MyClass | |
Interface: explicit | |
object: Demo.MyClass | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Demo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var myClass = new MyClass(); | |
Console.WriteLine("Class: " + myClass.ToString()); | |
Console.WriteLine("Interface: " + ((MyInterface)myClass).ToString()); | |
Console.WriteLine("object: " + ((object)myClass).ToString()); | |
Console.ReadLine(); | |
} | |
} | |
public interface MyInterface | |
{ | |
string ToString(); | |
} | |
public class MyClass : MyInterface | |
{ | |
public new string ToString() | |
{ | |
return "new"; | |
} | |
} | |
} | |
/* | |
displays | |
Class: new | |
Interface: new | |
object: Demo.MyClass | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Demo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var myClass = new MyClass(); | |
Console.WriteLine("Class: " + myClass.ToString()); | |
Console.WriteLine("Interface: " + ((MyInterface)myClass).ToString()); | |
Console.WriteLine("object: " + ((object)myClass).ToString()); | |
Console.ReadLine(); | |
} | |
} | |
public interface MyInterface | |
{ | |
string ToString(); | |
} | |
public class MyClass : MyInterface | |
{ | |
} | |
} | |
/* | |
displays | |
Class: Demo.MyClass | |
Interface: Demo.MyClass | |
object: Demo.MyClass | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Demo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var myClass = new MyClass(); | |
Console.WriteLine("Class: " + myClass.ToString()); | |
Console.WriteLine("Interface: " + ((MyInterface)myClass).ToString()); | |
Console.WriteLine("object: " + ((object)myClass).ToString()); | |
Console.ReadLine(); | |
} | |
} | |
public interface MyInterface | |
{ | |
} | |
public class MyClass : MyInterface | |
{ | |
public override string ToString() | |
{ | |
return "override"; | |
} | |
} | |
} | |
/* | |
displays | |
Class: override | |
Interface: override | |
object: override | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Demo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var myClass = new MyClass(); | |
Console.WriteLine("Class: " + myClass.ToString()); | |
Console.WriteLine("Interface: " + ((MyInterface)myClass).ToString()); | |
Console.WriteLine("object: " + ((object)myClass).ToString()); | |
Console.ReadLine(); | |
} | |
} | |
public interface MyInterface | |
{ | |
string ToString(); | |
} | |
public class MyClass : MyInterface | |
{ | |
public override string ToString() | |
{ | |
return "override"; | |
} | |
} | |
} | |
/* | |
displays | |
Class: override | |
Interface: override | |
object: override | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment