C # system. Object

Source: Internet
Author: User
System. Object
All classes in C # are directly or indirectly inherited from the system. Object Class, which enables the class in C # to be inherited by a single root. If we do not understand how to specify an inheritance class, the compiler considers that the class inherits from the system. Object Class by default. The system. object class can also be represented by the lower-case objectkeyword. The two are completely the same.
Because all the classes inherit from system. Object, these classes can all ask protected members and public members in the class. Let's take a look at the available members.

1. Public object ()
The system. Object Type constructor can directly use new object () to create objects. If it is not of the object type, the constructor of the derived type Automatically calls the constructor.

2. Public Virtual bool equals (Object OBJ)
Compare the objects that call this method with another object. If they are equal, true is returned. The default implementation code will check whether the same object is referenced by the number of objects (because the object is of the reference type ). If you want to compare the struct object in different ways, you can override this method.
For example, the state of two objects is public static bool equals (Object obja, object objb)
The static method equals (Object obja, object objb) first checks whether both objects obja and objb are null. If yes, true is returned. Otherwise, obja is performed. equals (objb) calls and returns its value. The problem comes down to the instance method equals (Object OBJ ). The default implementation of this method is actually {return this = OBJ;}, that is, to infer whether two objects are referenced to be equal.
This method returns the true condition: obja and objb are the same instance or obja and objb are both null or obja. Equals (objb)

3. Public Virtual int gethashcode ()
Used as a hash function of an object. This is a required function. It returns the status value of an object identified in the compressed form. It is applicable to hash algorithms and data structures such as hash tables.
If the two objects have the same result than the values, the gethashcode method of each object must return the same value. However, if the two objects have different values than the values, the gethashcode method of the two objects does not necessarily return different values.
The gethashcode method of an object must always return the same hash code, provided that the object state has not been changed. The object state is used to determine the return value of the equals method of the object. Note that this only applies to the current execution of the application. When you execute the application again, another hash code may be returned.

4. Public type GetType ()
Returns the object type in the form of a system. Type object.

namespace ConsoleApplication4{    class Program    {        static void Main(string[] args)        {            MyBaseClass myBase=new MyBaseClass();            MyDerivedClass myDerived=new MyDerivedClass ();            object o=myDerived ;            MyBaseClass b=myDerived;            Console.WriteLine("mybase:Type is {0}",myBase.GetType ());            Console.WriteLine("myDerived:Type is {0}",myDerived .GetType ());            Console.WriteLine("object o=myDerived:Type is {0}",o.GetType ());            Console.WriteLine("MyCassClass b=myDerived:Type is {0}",b.GetType ());        }    }    public class MyBaseClass:object     {    }    public class MyDerivedClass:MyBaseClass     {    }


5. Public static bool referenceequals (Object obja, object objb)
This method is similar to the two objects that the consumer sends to it to see if they are referenced by the same instance. Assume that obja and objb are the same instances, or if both are empty references, the value is true; otherwise, the value is false.

class Program    {        static void Main(string[] args)        {            object o = null;            object p = null;            object q = new object();            Console.WriteLine(object.ReferenceEquals(o, p));            p = q;            Console.WriteLine(object.ReferenceEquals(p, q));            Console.WriteLine(object.ReferenceEquals(o, p));         }    }


6. protected object memberwiseclone ()
Create a new object instance and copy members to copy the object. Copying members does not obtain new instances of these members. No matter what reference type member of the new object will reference the same object in the source class, this method is protected, so it can only be used in the class or derived class

class MyDerivedClass:MyBaseClass     {        static void Main(string[] args)        {            MyDerivedClass m1 = new MyDerivedClass();            m1.age=42;            m1.name="Sam";            MyDerivedClass m2 = (MyDerivedClass)m1.MemberwiseClone();                        Console.WriteLine(m2.name);            Console.WriteLine(m2.age);        }    }    class MyBaseClass    {        public string name;        public int age;    }


7. Public Virtual string tostring ()
Returns a string corresponding to the instance. By default, this is a qualified name of the class type, but it can be rewritten to provide an appropriate implementation method for the type. By default, the full name of the object type is returned. The inheritance class can override this method to display output content by itself. If the inheritance class needs to control many other formatting outputs, You need to implement the iformattable interface.








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.