What is the difference between == and equals method in c




















Remember how I mentioned that each type decides how it implements the Equals method. In the case of most primitive types, they choose not to override the method. The language bases its results on the memory location of our values and not their numeric values.

Well, comparing numeric values with Equals did not get us the results we expected. What results do you expect comparing strings with the Equals method? In this case, they are equivalent. A nice bonus of using the Equals operator on strings, is it comes overloaded to take a StringComparison enum to make case insensitive comparisons.

Note: be careful that x is not null, or else your application will throw a null reference exception. Comparing values is fundamental to all programming languages, and C is no different.

You have many options when deciding to compare two values together. Additionally, being an object-oriented language, many classes can be broken down to the most basic of primitives and compared. The Equals method is useful for strings but I advise not to use it on other classes unless you know the expected behavior.

I also do not recommend overriding the base implementation in your classes. If you want to make comparisons between more complex classes, I recommend writing a precise method outside of the instances. Khalid is a developer advocate at JetBrains focusing on.

NET technologies and tooling. Photo by Raquel Martinez. But look at the value, it the same. So C string follows interning rule. But now look at the below code where we are explicitly creating new separate objects of string with same value. We are forcing and overriding interning behavior of string. This is one place where the equality behavior differs. The next point which makes them different is when do type checks happen.

Now comparison is of two types one is purely based on content and reference, means computer based comparison and other is based on semantics. But I tried to use like this. That's not true due to string interning.

The C specification guarantees that - from section 2. So in this particular case, you would still get "true" even if you printed object. Overloading is a compile-time decision - the implementation to use depends on the compile-time types of the operands. So for example:. Compare that with object. Equals object which is a virtual method.

As it happens, String overloads this method as well , but importantly it overrides it. So if we change our code to:. Equals object , but they'll still both print True because of polymorphism: the implementation in String will be used. It's a holdover from C, in which it makes a difference. Any time you want to refer to a Foo , you'd always have to call it a struct Foo. This gets annoying fast, so you can add a typedef :. Now struct Foo in the tag namespace and just plain Foo in the ordinary identifier namespace both refer to the same thing, and you can freely declare objects of type Foo without the struct keyword.

Thus, with this construct, it doesn't have a name in the tag namespace, only a name in the typedef namespace. This means it also cannot be forward-declared. If you want to make a forward declaration, you have to give it a name in the tag namespace. See Michael Burr's answer for the full details.



0コメント

  • 1000 / 1000