The value type and reference type (Special string) Typeof and GetType () Use of static and non-static parameters to pass related knowledge, typeofgettype

Source: Internet
Author: User

The value type and reference type (Special string) Typeof and GetType () Use of static and non-static parameters to pass related knowledge, typeofgettype

Learning blog links: http://www.cnblogs.com/zhili/category/421637.html

1. Value Type and reference type

Note that string is a special type of reference.

Usage: = compares the values in the stack, the value type comparison value, and the object (except the string) compares the addresses in the stack.

Equal compares actual values and overwrites Virtual Methods in the object. It is best to overwrite the getHashCode () method during rewriting.

Example code

Static void Main (string [] args) {object m1 = 1; object m2 = 1; Console. writeLine (m1 = m2); // False; because the reference is compared, it is definitely not the same object. console. writeLine (m1.Equals (m2); // True. because the value of the object is compared. console. read () ;}example 2: static void Main (string [] args) {string str1 = "ZhangSan"; string str2 = "ZhangSan "; string str3 = new string (new char [] {'Z', 'H'}); string str4 = new string (new char [] {'Z ', 'H'}); Console. writeLine ("str1 = str2" + (str1 = str2 ). toString (); // True Console. writeLine ("str1 Equals str2" + str1.Equals (str2); // True Console. writeLine ("str3 = str4" + (str3 = str4 ). toString (); // True Console. writeLine ("str3 Equals str4" + str3.Equals (str4); // True. console. read ();} // conclusion: The = method is overwritten by string. used to compare values.

Difference between Typeof and GetType ()

Static void Main (string [] args) {object m1 = 1; object m2 = 1; // ValueType is a reference type. Because it is a class, false Console is returned. writeLine (typeof (ValueType ). isValueType); // False. valueType is a class !!! Console. WriteLine (m1.GetType (). IsValueType); // Ture Console. Read ();}

Three static and non-static.

The more thorough said is the link: ([C # Basic Knowledge series] comprehensive analysis of C # in static and non static) http://www.cnblogs.com/zhili/archive/2013/06/16/StaticAndNonStatic.html

My personal usage Summary: I want to deploy a category --> Create a static category --> static Attributes --> static members are private --> static methods are generally used as common tool classes.

Summary: 1. Static methods cannot be identified as virtual, abstract, or override. Static methods can be derived for access, but cannot be overwritten by Derived classes.

2. constructor execution process:

Static Members allocate memory space --> static member initialization --> execute static constructor --> execute instance Member initialization --> execute instance Constructor

In IL,. ctor represents the instance constructor.

Four parameter transfer

For value-based transmission, both the value type and the reference type are passed as a copy of the real parameter.

Only when the value type is used, a copy of the real parameter instance is passed (that is, a copy of the Value Type value)

For the reference type, the copy of the passed real parameter reference.

For pass by reference, the parameter address is passed, that is, the instance pointer. (add the ref and out keyword)

Note: string is a special reference type. The transfer is the actual value. To pass a reference through a method, that is, if the value outside is changed, the ref out parameter must be added.

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.