I am trying to use an extension method to implement ToString()
in a class that doesn't have it. If I change the method declaration to ToString2()
, it works fine, but when I try to use .ToString
it fails. Why does ToString2()
work, but not ToString()
?
Error:
System.NullReferenceException: Object reference not set to an instance of an object.
Here's my method implementation:
namespace LeankitExtensions
{
public static class CardViewExt
{
public static string ToString(this CardView c)
{
if (c == null)
return "null card";
return String.Format("-------\nexternal id: {0} \ntitle: {1}\n-------",c.ExternalCardID,c.Title);
}
}
}
Calling the method:
CardView cv = new CardView(){ExternalCardID="22";Title="Hi"};
Console.WriteLine(cv.ToString());