473,385 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How Object.Equals() implemented?

Hi Guys,

I was going through the source code of Object.cs in rotor. What I found is
Equals() implemented as follows:

[MethodImplAttribute(MethodImplOptions.InternalCall )]

public extern virtual bool Equals(Object obj);

What I don't understand is:

A) where is the actual code implemented?

B) What does 'extern' mean in this context?

Thanks in advance.

Bijay


Jul 19 '05 #1
1 3182
For your convinence, I am copying the Object.cs contents: (Please pay
attention to methods with 'extern' specifier. )

// ==++==
//
//
// Copyright (c) 2002 Microsoft Corporation. All rights reserved.
//
// The use and distribution terms for this software are contained in the
file
// named license.txt, which can be found in the root of this
distribution.
// By using this software in any fashion, you are agreeing to be bound by
the
// terms of this license.
//
// You must not remove this notice, or any other, from this software.
//
//
// ==--==
/*================================================= ===========
**
** Class: Object
**
**
**
** Object is the root class for all CLR objects. This class
** defines only the basics.
**
** Date: January 29, 1998
**
================================================== =========*/

namespace System {
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using CultureInfo = System.Globalization.CultureInfo;
using FieldInfo = System.Reflection.FieldInfo;
using BindingFlags = System.Reflection.BindingFlags;
using RemotingException = System.Runtime.Remoting.RemotingException;
// The Object is the root class for all object in the CLR System. Object
// is the super class for all other CLR objects and provide a set of methods
and low level
// services to subclasses. These services include object synchronization
and support for clone
// operations.
//
//This class contains no data and does not need to be serializable
/// <include file='doc\Object.uex' path='docs/doc[@for="Object"]/*' />
[Serializable()]
public class Object
{
// Creates a new instance of an Object.
/// <include file='doc\Object.uex'
path='docs/doc[@for="Object.Object"]/*' />
public Object()
{
}

[MethodImplAttribute(MethodImplOptions.InternalCall )]
private extern Type InternalGetType();
[MethodImplAttribute(MethodImplOptions.InternalCall )]
private extern Type FastGetExistingType();
// Returns a String which represents the object instance. The default
// for an object is to return the fully qualified name of the class.
//
/// <include file='doc\Object.uex'
path='docs/doc[@for="Object.ToString"]/*' />
public virtual String ToString()
{
return GetType().FullName;
}

// Returns a boolean indicating if the passed in object obj is
// Equal to this. Equality is defined as object equality for reference
// types and bitwise equality for value types using a loader trick to
// replace Equals with EqualsValue for value types).
//
/// <include file='doc\Object.uex'
path='docs/doc[@for="Object.Equals"]/*' />
[MethodImplAttribute(MethodImplOptions.InternalCall )]
public extern virtual bool Equals(Object obj);

/// <include file='doc\Object.uex'
path='docs/doc[@for="Object.Equals1"]/*' />
public static bool Equals(Object objA, Object objB) {
if (objA==objB) {
return true;
}
if (objA==null || objB==null) {
return false;
}
return objA.Equals(objB);
}

/// <include file='doc\Object.uex'
path='docs/doc[@for="Object.ReferenceEquals"]/*' />
public static bool ReferenceEquals (Object objA, Object objB) {
return objA == objB;
}

// GetHashCode is intended to serve as a hash function for this object.
// Based on the contents of the object, the hash function will return a
suitable
// value with a relatively random distribution over the various inputs.
//
// The default implementation returns the sync block index for this
instance.
// Calling it on the same object multiple times will return the same
value, so
// it will technically meet the needs of a hash function, but it's less
than ideal.
// Objects (& especially value classes) should override this method.
//
/// <include file='doc\Object.uex'
path='docs/doc[@for="Object.GetHashCode"]/*' />
[MethodImplAttribute(MethodImplOptions.InternalCall )]
public extern virtual int GetHashCode();

// Returns a Type object which represent this object instance.
//
/// <include file='doc\Object.uex'
path='docs/doc[@for="Object.GetType"]/*' />
public Type GetType()
{
Type ret;
ret = FastGetExistingType();

if (ret == null)
ret = InternalGetType();
return ret;
}

// Allow an object to free resources before the object is reclaimed by
the GC.
//
/// <include file='doc\Object.uex'
path='docs/doc[@for="Object.Finalize"]/*' />
~Object()
{
}

// Returns a new object instance that is a memberwise copy of this
// object. This is always a shallow copy of the instance. The method is
protected
// so that other object may only call this method on themselves. It is
entended to
// support the ICloneable interface.
//
/// <include file='doc\Object.uex'
path='docs/doc[@for="Object.MemberwiseClone"]/*' />
[MethodImplAttribute(MethodImplOptions.InternalCall )]
protected extern Object MemberwiseClone();
// Sets the value specified in the variant on the field
//
private void FieldSetter(String typeName, String fieldName, Object val)
{
// Extract the field info object
FieldInfo fldInfo = GetFieldInfo(typeName, fieldName);

// Make sure that the value is compatible with the type
// of field
System.Runtime.Remoting.Messaging.Message.CoerceAr g(val,
fldInfo.FieldType);

// Set the value
fldInfo.SetValue(this, val);
}

// Gets the value specified in the variant on the field
//
private void FieldGetter(String typeName, String fieldName, ref Object
val)
{
// Extract the field info object
FieldInfo fldInfo = GetFieldInfo(typeName, fieldName);

// Get the value
val = fldInfo.GetValue(this);
}

// Gets the field info object given the type name and field name.
//
private FieldInfo GetFieldInfo(String typeName, String fieldName)
{
Type t = GetType();
while(null != t)
{
if(t.FullName.Equals(typeName))
{
break;
}

t = t.BaseType;
}

if (null == t)
{
throw new RemotingException(String.Format(
Environment.GetResourceString("Remoting_BadType"),
typeName));
}

FieldInfo fldInfo = t.GetField(fieldName, BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.IgnoreCase);
if(null == fldInfo)
{
throw new RemotingException(String.Format(
Environment.GetResourceString("Remoting_BadField") ,
fieldName, typeName));
}

return fldInfo;
}
}
}
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object class (Object.cs in rotor). What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What...
2
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...
17
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, ...
3
by: cmrchs | last post by:
Hi, why is it requested that when Equals() is implemented in a class that GethashCode() must be implemented as well ? thnx Chris ...
2
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't...
1
by: MariusI | last post by:
I have some business objects which overrides Equals to provide syntax equality instead of just reference equality. Overriding equals gives me a warning that i must override GetHashcode(). Msdn says...
8
by: Alexander Walker | last post by:
Hello I am using a Dictionary to store some queues which store instances of a class that I have written I would like to be able to determine whether a particular queue already contains an...
4
by: nick_nw | last post by:
Hi All, Just been re-reading Applied MS .Net Framework Programming (been about 2 years since I last read it) and came across the chapter on Equals, ==, and GetHashCode. It dawned on me that...
5
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.