Yeah, I realise I can check for the NULL first, but I want to be able to
I think you are asking for nullable types which will be available in C# 2.0.
Support for nullable types was added to have the language interact with
databases.
Hence you will in the near future be able to write:
int? a = 5;
int? b = null;
int? c = a + b;
C would then contain null.
That's exactly what I was asking, thanks.
Another question on nulls. I'm trying to override the == for a class I have
created:
public class Termination
{
public string CountryName, TerminationName;
public Termination(string countryName, string terminationName)
{
this.CountryName = countryName; this.TerminationName = terminationName;
}
public override string ToString()
{
string str = this.CountryName;
if(this.TerminationName != "" && this.TerminationName != null)
str += " - " + this.TerminationName;
return str;
}
// .................
public static bool operator==(Termination t1, Termination t2)
{
return (t1 == null && t2 == null) ||
(t1 != null && t2 != null && (String.Compare(t1.ToString(),
t2.ToString()) == 0));
}
// .................
}
This is all good if I do this:
Termination t1 = new Termination("USA", "Washington");
Termination t2 = new Termination("UK", "London");
Console.WriteLine(t1 == t2); // False
But this throws "An unhandled exception of type
'System.StackOverflowException'":
Termination t1 = null;
Console.WriteLine(t1 == null); // should be true
presumably because I am defining the == operator WITH the == operator,
causing infinite recursion at runtime. I originally tried this for my
override method:
public static bool operator==(Termination t1, Termination t2)
{
return (String.Compare(t1.ToString(), t2.ToString()) == 0));
}
but that gives "An instance of the object not set" at runtime when comparing
t1 to null.
How do I avoid this?
Mike