base class and Access modifiers for C #

Source: Internet
Author: User
Tags bool data structures hash modifier modifiers resource tostring

base class and Access modifiers for C #

A The base class of C # System. Object

In the object-oriented programming process, all classes are super parent system. The subclass of the object class. The system is automatically inherited by all classes. Object, the system is also inherited by nature. All methods of the object class, System. object contains methods such as:

public static bool Equals (Object Obja,object objb) {}
public static bool ReferenceEquals (Object Obja,object objb) {}
Public Object () {}
Public virtual bool Equals (object obj) {}
public virtual int GetHashCode () {}
Public Type GetType () {}
Public virtual string ToString () {}
protected virtual void Finalize () {}
Protected Object MemberwiseClone () {}

The first explanation is that the meaning of these access modifiers, such as the modifier public,static,protected,virtual,private before the method, is described below, and we now look at the specific effects of these methods, but many of these methods are rarely used, The more you use are the ToString () and GetHashCode () methods.

ToString () Method: Converts an object that calls the method to a string and then assigns a value to another variable

Double F = math.pi;

string Tostring = null;

Tostring = F.tostring ();

Console.WriteLine ("Double f={0}", f);

Console.WriteLine ("Call ToString = F.tostring ()");

Console.WriteLine ("Tostring={0}", Tostring); Console.WriteLine ("The value of D after the call ={0}", f);

The results of the left program running are as follows:

GetHashCode () Method: serves as a hash function of a particular type. GetHashCode is suitable for use in hashing algorithms and data structures such as hash tables

String string1 = "Ludei";

String string2 = "Ludei";

String string3 = string1;

Console.WriteLine ("Str1.") GETHASHCODE{0} ", string1. GetHashCode ());

Console.WriteLine ("Str2.") Gethashcode{0} ", string2. GetHashCode ());

Console.WriteLine ("Str3.") Gethashcode{0} ", String3. GetHashCode ());

GetType () Method: This method returns from system. An instance of a class derived from type. This object can provide more information about the class to which the object belongs, including basic types, methods, properties, and so on.

Finalize () method: Called when the referenced object is reclaimed to clean up the resource. The object execution code of the Finalize () method does not actually do anything, and is ignored by the garbage collector. If the object has a reference to an unmanaged resource, you need to remove the references when the object is deleted, and you will typically override Finalize (). The garbage collector cannot override this method directly because it is only responsible for the managed resources and relies on the Finalize () provided by the user.

Equals (Object Obja,object objb) and ReferenceEquals (Object Obja,object OBJB) The return value of the two methods is bool, which compares two objects to be the same object. Difference is

Equals (Object Obja,object OBJB): If the two references to the comparison point to the same object, the method returns True, otherwise it returns false, and the ReferenceEquals method is used to determine whether two references point to the same object. Which is the reference equality

Two Access modifiers for C #

C # 's access modifiers are a few: protected, private, public, internal, protected internal. Adding these modifiers before a class or a member of a class restricts the program's access to code with modifiers. Now introduce the method characteristics of the modifiers:

Public, the way to select it is to include the public modifier in the member declaration. The intuitive meaning of public is "access is unrestricted." All of the code can be accessed

Protected, the way to select it is to include the Protected modifier in the member declaration. The intuitive meaning of protected is that the access scope is limited to the class to which it belongs or the type derived from the class.

Internal, the way to select it is to include the Internal modifier in the member declaration.  The intuitive meaning of internal is that "access is limited to this program," which is the current program to access. Protected internal (meaning protected or internal), the way to select it is to include the Protected and internal modifiers in the member declaration. The intuitive meaning of protected internal is that the access scope is limited to this program or to those types that derive from the class to which it belongs.

Private, the way to select it is to include the private modifier in the member declaration. The intuitive meaning of private is that the access scope is limited to the type to which it belongs. This is the default if you are not modifying a class or member during the programming process.

Concepts about value types and reference types: If a variable VA1 store a value type, it stores the object that contains the data directly. Any other variable cannot directly store an object that has been stored by VA1, although VAR2 can store an object with the same value as the var1 stored object, but this means that it has nothing to do with the stored object, and any changes to one of the stored objects will not affect the object stored by the other variable. If the variable var1 store a reference type, it stores a reference to the object directly, while there may be another variable var2, which also stores a reference to the object, which means that Changing the object referenced by the VAR2 also affects the objects referenced by the var1. Here is a simple introduction, in the future will provide detailed examples.

Related Article

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.