Re: Comparison to null
From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 03/15/05
- Next message: Bob Grommes: "Re: default value assigned to SqlParameter that can be overwritten"
- Previous message: Nicholas Paldino [.NET/C# MVP]: "Re: Using C# to insert blob into Oracle"
- In reply to: Marcin Grzębski: "Re: Comparison to null"
- Next in thread: Marcin Grzębski: "Re: Comparison to null"
- Reply: Marcin Grzębski: "Re: Comparison to null"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 15 Mar 2005 17:51:14 -0000
Marcin Grz?bski <mgrzebski@taxussi.no.com.spam.pl> wrote:
> I think that a "key" is operator overloading.
> Like a Jon post it before.
>
> MyClass myVar;
>
> if( null==myVar ) {
> }
> Doesn't have a chance to fire MyClass "==" operator function.
Yes it does. Compile and run the following, ignoring the warnings:
using System;
using System.Data;
class Test
{
static void Main()
{
Test t = new Test();
Console.WriteLine (t==null);
Console.WriteLine (null==t);
}
public static bool operator == (Test t1, Test t2)
{
Console.WriteLine ("Operator== called");
Console.WriteLine ("t1={0}", t1);
Console.WriteLine ("t2={0}", t2);
return true;
}
public static bool operator!= (Test t1, Test t2)
{
return false;
}
}
The output is:
Operator== called
t1=Test
t2=
True
Operator== called
t1=
t2=Test
True
In other words, the overloaded operator is called both times.
-- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
- Next message: Bob Grommes: "Re: default value assigned to SqlParameter that can be overwritten"
- Previous message: Nicholas Paldino [.NET/C# MVP]: "Re: Using C# to insert blob into Oracle"
- In reply to: Marcin Grzębski: "Re: Comparison to null"
- Next in thread: Marcin Grzębski: "Re: Comparison to null"
- Reply: Marcin Grzębski: "Re: Comparison to null"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|