|
| Recent
Articles |

C#: Realtime Currency Exchange Class Soon, I'm facing a new project where the need for up-to-date currency exchanges is crucial. This is not the first time I've done this, but I wanted to make a reusable, self-maintainable class so I don't have to do it again. The European Central Bank (ECB) provides...
Validating Strong Passwords In C# And ASP.NET It's always a good idea to have a password policy when creating new applications. A password policy can vary from project to project, but the important part is just to have one to begin with. It is very difficult to implement...
Retrieve Subdomain From A URL In C# I had to come up with a method that retrieved the subdomain from the current web request on an ASP.NET website. I thought that the System.Uri class contained that information in an easy retrievable way, but no. Here's what I came up with instead. It still uses the System.
Working With Weeks In C# (Not That Obvious) For some reason, Microsoft didn't add a Week property to the DateTime class. I never could figure out why. Instead they gave us the System.Globalization namespace, filled with date related functionality like the different...
Creating Safe Events In C# Whenever you write class libraries, custom control or just about anything else, you probably raise a lot of home made events. That's a simple thing to do, but tedious to write over and over again. That's why I always use this snippet for Visual Studio 2005 that writes the...
Assigining a TypeConverter to a Class you Don't Own I ran into problems with the XNA Beta1, where by I had a class that had a Vector2 struct in. The problem with the XNA Vector2 struct is that there is no TypeConverter for it at the moment. This means that there is no...
Rational Betas Have C++ Goodies Features in the Rational Software Architect v7.0 beta
include improved support for C++, and developers can try out the open beta by pre-registering for it with IBM. Open betas for IBM Rational Software Architect V7.0 and Rational Functional Tester V7.0 are scheduled...
Revisiting C/C++ On Eclipse The tutorial from IBM's developerWorks on using the C/C++ Development Toolkit (CDT) for Eclipse received a refresh over the summer, and it merits a look from those who may have missed it since then.n the writeup...
Using Immediate Window to Work with Values This article is an excerpt from the book: Murach's ASP.NET 2.0 Web Programming with C# 2005. The Immediate window, shown in figure 4-14, is useful for displaying the values of variables or properties that don't appear in the Code Editor window. To display a value, you...
|
 |
|
|
01.18.07 Overload Operators The Right Way In C# By
Mads Kristensen It's a good rule of thumb to overload the equality operators on classes.
That ensures a correct comparison between to class instances of the same type. If you don't, .NET automatically uses reflection and that is way slower than a custom implementation.
We all use the equality operators ("==", "!=") all the time and we expect them to be right every time. They are not!, but even if they were there is a good chance that you want to change them anyway. For instance, if your class has a unique Id property, then that property is the one that tells if two instances are the same. If you don't overload the operators, you could have two instances with the same Id treated as two different instances.
First of all, you have to find out what makes two instances different. Let's keep using the Id property example in an imaginary class called Foo. Then lets start overriding the GetHashCode() method so it returns the hashcode of the Id property.
Whenever the GetHashCode() method is called, it returns a unique integer value that represents the uniqueness of the class instance. Now we can use that method to overload the operators.
The reason why it is necessary to box the two Foo parameters (first, second) to type object, is to avoid an overflow exception.
We still need one more thing to be sure that we can compare two instances to each other, and that is by overriding the Equals() method. You can use the Equals() method or the operators to compare instances and they should always return the same result.
You can implement these methods on a base class so you
won't have to write it on all the derived classes. Because these methods uses GetHashCode() to compare two instances, it would be preferable to override that method in the derived classes if needed.
Comments
About the Author: Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.
|
|