January 2007 - Posts

Testing Equality in C#

Theres many ways to test equality between two object in fact theres four we have Object.ReferenceEquals(), static Equals(), instance Equals(), operator==

Object.ReferenceEquals();

This method simple test the equality of referances of two referance types so if these two types are pointing to the same referance so they forsure pointing to the same type with same content, however if you tried to compare between two value types you will get false always even if compare one type to it self like line 4 in the following code.

1
2
3
4
5
6
int n1 = 10;
int n2 = 10;

Object.ReferenceEquals( n1,n2 ); //always return false

Object.ReferenceEquals( n1,n1 ); //always return false

static Equals()

This one is tough this static method is inhirted from the base class Object simply calling Equals() delegate to the implementation of one of the types being compared.

this is how Equals() could be implemented

1
2
3
4
5
6
7
8
9
10
public static bool Equals( object left, object right )                             
{
// Check object identity
if (left == right )
return true;
// both null references handled above
if ((left == null) || (right == null))
return false;
return left.Equals (right);
}

where the actual responsibility is delegated to left object. remember that you can't override this function.

instance Equals()

Now what if you created a value type for example and you want to defind your own custom way to test equality this is when you shoud do some actual coding.. overide the instance method Equals() remember that you don't have to implement your own version of Equals() unless the default implementation don't match your desire or not work best for your created value type.

This example show how you may implement your own Equals()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ublic class Foo                                                                  
{
public override bool Equals( object right )
{
if (right == null)
return false;

if (object.ReferenceEquals( this, right ))
return true;

if (this.GetType() != right.GetType())
return false;

return CompareFooMembers(
this, right as Foo );
}
}

In the first check (line 5) we check if the object has value, the second comparison we try to match the two object referances (line 7) and last comparison in (line 11) we compare the Foo members (content) ...anyway so what about the (line 9).

One of the properties of equality is symmetric but this is not the case here. in the coming peace of code we will compare two types Base type and Derived type and see how symmetrically is an issue here.

Class B - The base class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class B                                                                     
{
public override bool Equals( object right )
{
// check null:
if (right == null)
return false;

// Check reference equality:
if (object.ReferenceEquals( this, right ))
return true;

// Problems here, discussed below.
B rightAsB = right as B;
if (rightAsB == null)
return false;

return CompareBMembers( this, rightAsB );
}
}

Class D - The drived class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class D : B                                                                 
{
// etc.
public override bool Equals( object right )
{
// check null:
if (right == null)
return false;

if (object.ReferenceEquals( this, right ))
return true;

// Problems here.
D rightAsD = right as D;
if (rightAsD == null)
return false;

if (base.Equals( rightAsD ) == false)
return false;

return CompareDMembers( this, rightAsD );
}

}

Test Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
B baseObject = new B();                                                           
D derivedObject = new D();

// Comparison 1.
if (baseObject.Equals(derivedObject))
Console.WriteLine( "Equals" );
else
Console.WriteLine( "Not Equal" );

// Comparison 2.
if (derivedObject.Equals(baseObject))
Console.WriteLine( "Equals" );
else
Console.WriteLine( "Not Equal" );

In the comparison one the condition will evalueate to true as the derived type can be converted implicty to the base type so it passes this condition at the implementation of the Equals() like

if (this.GetType() != right.GetType()) return false;

however this is not vice versa as the base type can't be casted to the derived.

operator==

This is very similar to instance Equals() you may have to overrid this operator to match the logic of equality behid your custom created value type.

but remember that any modification of operator== or instance Equals() may affect other equality function.

More information on testing equality

kick it on DotNetKicks.com
Posted by Adel Khalil with 1 comment(s)
Filed under: ,

My Own Agenda for MDC 2007

What i will attend ?

Every year since Middle East Developer Conference started i planned ahead what i will attend and as theres many intresting concurrent sessions i face hard time favor session over other. They try to divide the session into distinct intrest regions but seems like impossible job for example it's almost typical that the developer that intrested in attending the LINQ session will be intrested to attend database related session anyway this year somehow i didn't have that every-year-hard-time choosing what i will attend and here's my first draft this also subject of change when i hit a promising session with bad speaker which is happend in MDC 04 couple of times.

So here where you can find me, if you do come on say Hi ;)

Date

http://www.mdc2007.com/images/space.gif

Start Time

http://www.mdc2007.com/images/space.gif

End Time

http://www.mdc2007.com/images/space.gif

Code

http://www.mdc2007.com/images/space.gif

Session

http://www.mdc2007.com/images/space.gif

Speaker

http://www.mdc2007.com/images/space.gif

Room

05 Feb 2007

http://www.mdc2007.com/images/space.gif

12:00 PM

http://www.mdc2007.com/images/space.gif

01:15 PM

http://www.mdc2007.com/images/space.gif

DEV

http://www.mdc2007.com/images/space.gif

Profile Guided Optimization

http://www.mdc2007.com/images/space.gif

Andrew Pardoe

http://www.mdc2007.com/images/space.gif

TBD

05 Feb 2007

http://www.mdc2007.com/images/space.gif

02:15 PM

http://www.mdc2007.com/images/space.gif

03:30 PM

http://www.mdc2007.com/images/space.gif

WEB

http://www.mdc2007.com/images/space.gif

Windows CardSpace and Identity Metasystem

http://www.mdc2007.com/images/space.gif

Rafal Lukawiecki

http://www.mdc2007.com/images/space.gif

TBD

05 Feb 2007

http://www.mdc2007.com/images/space.gif

04:00 PM

http://www.mdc2007.com/images/space.gif

05:15 PM

http://www.mdc2007.com/images/space.gif

DEV

http://www.mdc2007.com/images/space.gif

Windows Communication Foundation

http://www.mdc2007.com/images/space.gif

TBD

http://www.mdc2007.com/images/space.gif

TBD

06 Feb 2007

http://www.mdc2007.com/images/space.gif

10:00 AM

http://www.mdc2007.com/images/space.gif

11:30 AM

http://www.mdc2007.com/images/space.gif

GEN

http://www.mdc2007.com/images/space.gif

Overview of Team Foundation Server

http://www.mdc2007.com/images/space.gif

VSTS Team

http://www.mdc2007.com/images/space.gif

Khofo

06 Feb 2007

http://www.mdc2007.com/images/space.gif

12:00 PM

http://www.mdc2007.com/images/space.gif

01:15 PM

http://www.mdc2007.com/images/space.gif

DEV

http://www.mdc2007.com/images/space.gif

Overview of LINQ

http://www.mdc2007.com/images/space.gif

Charlie Calvert

http://www.mdc2007.com/images/space.gif

TBD

06 Feb 2007

http://www.mdc2007.com/images/space.gif

02:15 PM

http://www.mdc2007.com/images/space.gif

03:30 PM

http://www.mdc2007.com/images/space.gif

DB

http://www.mdc2007.com/images/space.gif

VSTS - DB PRO

http://www.mdc2007.com/images/space.gif

TBD

http://www.mdc2007.com/images/space.gif

TBD

06 Feb 2007

http://www.mdc2007.com/images/space.gif

04:00 PM

http://www.mdc2007.com/images/space.gif

05:15 PM

http://www.mdc2007.com/images/space.gif

DB

http://www.mdc2007.com/images/space.gif

SQL CLR in Action

http://www.mdc2007.com/images/space.gif

Stephen Forte/Richard Campbell

http://www.mdc2007.com/images/space.gif

TBD

07 Feb 2007

http://www.mdc2007.com/images/space.gif

10:00 AM

http://www.mdc2007.com/images/space.gif

11:30 AM

http://www.mdc2007.com/images/space.gif

GEN

http://www.mdc2007.com/images/space.gif

Developers Communities in egypt

http://www.mdc2007.com/images/space.gif

http://www.mdc2007.com/images/space.gif

Khofo

07 Feb 2007

http://www.mdc2007.com/images/space.gif

12:00 PM

http://www.mdc2007.com/images/space.gif

01:15 PM

http://www.mdc2007.com/images/space.gif

DEV

http://www.mdc2007.com/images/space.gif

Ngen to build more performant managed applications

http://www.mdc2007.com/images/space.gif

Surupa Biswas

http://www.mdc2007.com/images/space.gif

TBD

07 Feb 2007

http://www.mdc2007.com/images/space.gif

02:15 PM

http://www.mdc2007.com/images/space.gif

03:30 PM

http://www.mdc2007.com/images/space.gif

WEB

http://www.mdc2007.com/images/space.gif

Windows Workflow Foundation

http://www.mdc2007.com/images/space.gif

Ahmed Badr

http://www.mdc2007.com/images/space.gif

TBD

07 Feb 2007

http://www.mdc2007.com/images/space.gif

04:00 PM

http://www.mdc2007.com/images/space.gif

05:15 PM

http://www.mdc2007.com/images/space.gif

DEV

http://www.mdc2007.com/images/space.gif

JIT-ing (Dynamic Compilation for managed code)

http://www.mdc2007.com/images/space.gif

Surupa Biswas

http://www.mdc2007.com/images/space.gif

TBD

 

MDC 07 Agenda

Posted by Adel Khalil with 1 comment(s)

Are you one of those ? ;)

One of my not-technical friends sent me email with this funny picture. thought i share and ask are you one of these guys ? ;)

kick it on DotNetKicks.com
Posted by Adel Khalil with 2 comment(s)
Filed under:

Surprise: Smooth installation of Visual Studio Service Pack 1 on Vista

All of you of course heared all the fuss about how hard, tricky and painfull the installation of Visual Studio SP1 is.

so as most of you guys i google the VSSP1 to find out how things gone for folks around the world and came with couple of usefull posts here and here.

The common advice which i found on almost every post/article talking about upgrading to SP1 is UNINSTALL the web project templates unless you want to spent half hour to see cute message telling you that you have to do before installation.

the surprise - thank god - was that i have a clean and smooth installation on Vista Build 6000 also installed the magic update of VS for Vista afterwards.

if you already have a beta of the SP1 installed on Vista here's how you can get rid of it.

You can download the SP1 form here and the VS update for Vista from here.

 

Posted by Adel Khalil with 1 comment(s)
Filed under: , ,

See you february!

Theres two hot conferences next month.. as Bashmohandes post about the 2007 version of Middle East Developer Conference. Which will be held at Cairo International Conference Center From February 4th to February 7th.

Although they announced the place and the date for the conference the events page at Microsoft Middle East still not updated. hope we got agenda in the upcoming days.

Also at the same time and same place theres Cairo ICT 2007. Registration at http://www.cairoict.com/ .

Posted by Adel Khalil with 1 comment(s)

Small utility help freelance developers

Hello, guys... back yet from the holiday.. the community is so quite these days... but anyway.. i'm writing alot freelance projects these days.. and i wanted a tiny tool help me calculate total of hours that i spent working on a project so as i missed the coding during the holidays i decided to write this one... not fancy just smal timer with a db and flexability.

The source code is included in case you wanna make any changes to the timer interval property :D

download the utillity stand alone, package or source code.

have fun.

kick it on DotNetKicks.com
Posted by Adel Khalil with 2 comment(s)
Filed under: , ,