Why can I overload operator== and operator!= separately having different
implementations and additionally I can override equals() also having a
different implementation.
Why not forbid overloading of == and != but instead translate each call
of objA==objB automatically in System.Object.E quals(objA, objB).
This would remove inconsistencies like
myString1==mySt ring2
and
(object)myStrin g1==(object)myS tring2
having different results.
also, should operator!= not always return the negated value of!=?
So why is it good for?
I would like to understand the technical reason for that, if there was any.
Jun 17 '06
12 2210
Barry Kelly wrote: cody <de********@gmx .de> wrote:
2. Even with reference types, you're overriding a method that takes object parameters, so your own Equals override will have to perform cumbersome type checking on the supplied objects. Sure it will have to, but honestly how often do you use == for objects that are *no* value types and *no* strings (which are sealed) both require no type checks. In collection methods like IndexOf the non-type safe version of Equals is used and type checks have to be performed anyway.
Referential equality for mutable types has completely different semantics from value equality, which is used for strings. With the greatest respect, I use referential equality far, far, far more often than value equality.
Taking something out of thin air:
Foo f = new Foo(); Foo f2 = new Foo(); Console.WriteLi ne(f == f2);
How often, in your programs, do you require this to print "True" on the console? Speaking for myself, almost *never*.
For most other cases you want reference equality anyway for which an operator=== could be used.
Here, it seems to me, you are introducing a new operator in order to do what most people already use '==' for.
Which IMO is bad style. You never know whether the given objects have an
overloaded operator or not, maybe one is added later which will change
semantics of your code. Currently you will have to use
Object.Referenc eEquals or (object)obj1==( object)obj2 to ensure reference
equality is done. And as I said, nobody prevents you from implementing a strongly typed version of Equals which will then be picked by the compiler and here also is no type check necessary.
But: this is advocating making an expensive boxing operation the default. That approach might be more reasonable in a more dynamic language like Lisp or Python, but it isn't right in a language with a C-based history.
Microsoft recommends in the newer guidelines to always add a strongly
typed Equals to classes anyway.
The current implementation of operator overloading doesn't allow generic
code making use of them since static methods like operators are, cannot
be specified in interfaces.
public T Add<T>(T a, T b)
where a,b : IAddable<T> // interface may implement + and -
{
return a+b; // internally calls a.Add(b)
}
This way, users can make interfaces which implements the operators they
need for specific operations. In languages like C++, D (Digital Mars),
Python and Ruby are also normal methods (C++ also allows static ones for
operators).
Mark Wilden <Ma********@new sgroups.nospam> wrote: Referential equality for mutable types has completely different semantics from value equality, which is used for strings.
Hmmm...I thought all identical strings were folded to the same reference. Or am I thinking of a completely different language?
That's interning you're thinking of, and while it automatically applies
to string *literals* it certainly doesn't apply to strings in general.
For instance:
using System;
class Test
{
static void Main()
{
string x = "hello".Substri ng (0, 1);
string y = "hi".Substr ing (0, 1);
Console.WriteLi ne (x==y);
Console.WriteLi ne (object.Referen ceEquals(x, y));
}
}
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... That's interning you're thinking of, and while it automatically applies to string *literals* it certainly doesn't apply to strings in general.
Thanks. I knew I was thinking of something... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: #Hai |
last post by:
Hi,
What is the difference between Object.Equals and "==" operator ?
When we use CollectionBase.List.Remove(object), which methods is used to
compare objects ?
Thanks
|
by: emma middlebrook |
last post by:
Hi
Having difficulty getting myself clear on how a type's operator== fits
in with Object.Equals.
Let's just consider reference types.
The default operator== tests for object identity (reference)
equivalence. You can override it if you like.
|
by: Zeng |
last post by:
I'm trying to comparing 2 objects (pointer to object) to see if they are the
"same" as each other. Here is what the definition of being the "same"
object type for both objects, object 1, object 2, same?
-----------------------------------------------------------
Decimal 199677 199677 yes
String "hello" "hello" yes
null null
yes
|
by: Fei Li |
last post by:
Hi,
take string class as an example, who can explain the difference?
Thanks
|
by: Kurt |
last post by:
Wouldn't you agree all of the follwoing should produce the same
result?
r = (o1 == o2);
r = (o2 == o1);
r = object.Equals(o1, o2);
r = object.Equals(o2, o1);
r = (o1.Equals(o2));
r = (o2.Equals(o1));
| |
by: Chris |
last post by:
This is something that has been bugging me for some time. When exactly
should I be using == instead of .Equals() ?
I see a lot of code that performs object evaluation e.g. Is Object1 the
same as Object2 by using == which works but shouldn't it be using
.Equals()? e.g. Object1.Equals(Object2)
I believe == should be using for simple type evaluation e.g.
int a = 0;
|
by: JohnR |
last post by:
From reading the documentation, this should be a relatively easy thing. I
have an arraylist of custom class instances which I want to search with
an"indexof" where I'm passing an instance if the class where only the
"searched" property has a value. I expected to get the index into the
arraylist where I could then get the entire class instance. However, the
'indexof' is never calling my overloaded, overrides Equals method. Here is
the...
|
by: Kenneth Baltrinic |
last post by:
When one overrides the Equals() method of an object, one is supposed to
override GetHashCode() as well and this makes good sense. But I have seen
lots of people who do this and do not override the == and != opperators. Am
I missing something or when would one want to have different implementations
for Equals and ==?
--Ken
|
by: r035198x |
last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize.
These were designed to be overridden according to specific general contracts. Other classes that make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly.
In this article I'll take a look at the equals and hashCode methods.
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |