Re: about IndexOf

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Thu, 24 Jan 2008 09:45:12 -0800, Jon Skeet [C# MVP] <skeet@xxxxxxxxx> wrote:

Version B. That way you've only got the actual equality check (for the
equality of "name" fields) in one place.

I agree with that generally (not having copied code). But it begs the question: why the need for the non-virtual Name.Equals(Name) method in the first place? If you've already got the virtual Name.Equals(Object) and it does the right thing, why bother with the other method?

I would think that just having a single method is better in this case, rather than having two, one of which just does the type check before calling the other. The type check isn't that expensive, if I recall correctly, nor is the overhead for the call to the virtual method, and avoiding those performance hits is the only reason I can think of for splitting the behavior up.

Are there places in .NET or C# where having the non-virtual Equals() method is particularly useful?

Pete
.