There is a simple class:
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
Test test = null;
int? t;
t = test?.i; // in this place the overloaded method "operator! =" is NOT called
if (test != null) // in this place the overloaded method "operator! =" is called
{
t = test.i;
}
}
}
public class Test
{
public int i = 5;
public override bool Equals(object obj)
{
return true;
}
public static bool operator ==(Test test1, Test test2)
{
return true;
}
public static bool operator !=(Test test1, Test test2)
{
return true;
}
}
}
In this line:
if (test != null)
called
public static bool operator !=(Test test1, Test test2)
but in this line:
t = test?.i;
none of the overloaded methods are called
How to overload operator "?."