J
Jongmin
Hi,
I have made two classes, which are simple and almost same,
T_A and T_B.
T_A has a static method overloaded to "==".
T_B dosn't.
And, I run the following code. I expect both "a" and "b"
show the messagebox. But, only "b" showes the messagebox.
Even I try to see "a" through visual studio, "a" is
<undefined value>, null. and "a == null" is "true".
Whats' wrong with it?
Thanks,
Jongmin
// ------> Code snippet
T_A a = null;
if (a == null)
MessageBox.Show("T_A is null"); // it is not shown.
T_B b = null;
if (b == null)
MessageBox.Show("T_B is null");
// ----> full Code
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
public class MyClass
{
public static void Main()
{
Test();
}
public static void Test()
{
T_A a = null;
if (a == null)
MessageBox.Show("T_A is null");
T_B b = null;
if (b == null)
MessageBox.Show("T_B is null");
}
}
public class T_A
{
private int _Idx = 0;
public int Idx
{
get {return _Idx;}
set {_Idx = value;}
}
public override string ToString()
{
return Idx.ToString();
}
public override int GetHashCode()
{
return this.Idx;
}
public override bool Equals(object obj)
{
if (obj==null) return false;
if (this.GetType() != obj.GetType())
return false;
T_A other = (T_A)obj;
return (this.Idx == other.Idx);
}
public static bool operator ==(T_A a, T_A b)
{
if ((object)a == null) return false;
return a.Equals(b);
}
public static bool operator !=(T_A a, T_A b)
{
return !(a==b);
}
}
public class T_B
{
private int _Idx = 0;
public int Idx
{
get {return _Idx;}
set {_Idx = value;}
}
}
I have made two classes, which are simple and almost same,
T_A and T_B.
T_A has a static method overloaded to "==".
T_B dosn't.
And, I run the following code. I expect both "a" and "b"
show the messagebox. But, only "b" showes the messagebox.
Even I try to see "a" through visual studio, "a" is
<undefined value>, null. and "a == null" is "true".
Whats' wrong with it?
Thanks,
Jongmin
// ------> Code snippet
T_A a = null;
if (a == null)
MessageBox.Show("T_A is null"); // it is not shown.
T_B b = null;
if (b == null)
MessageBox.Show("T_B is null");
// ----> full Code
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
public class MyClass
{
public static void Main()
{
Test();
}
public static void Test()
{
T_A a = null;
if (a == null)
MessageBox.Show("T_A is null");
T_B b = null;
if (b == null)
MessageBox.Show("T_B is null");
}
}
public class T_A
{
private int _Idx = 0;
public int Idx
{
get {return _Idx;}
set {_Idx = value;}
}
public override string ToString()
{
return Idx.ToString();
}
public override int GetHashCode()
{
return this.Idx;
}
public override bool Equals(object obj)
{
if (obj==null) return false;
if (this.GetType() != obj.GetType())
return false;
T_A other = (T_A)obj;
return (this.Idx == other.Idx);
}
public static bool operator ==(T_A a, T_A b)
{
if ((object)a == null) return false;
return a.Equals(b);
}
public static bool operator !=(T_A a, T_A b)
{
return !(a==b);
}
}
public class T_B
{
private int _Idx = 0;
public int Idx
{
get {return _Idx;}
set {_Idx = value;}
}
}