CLR Vir C # type Basics

Source: Internet
Author: User

 All classes are ultimately inherited from system. object. 

Therefore, the inheritance class will have the object's instance method:

1. equals

2. gethashcode

3. tostring

4. GetType

5. memberwiseclone.

6. Finalize

Where tostring () With the help of the reflector tool, we can see its definition in the object:

Code
Public Virtual StringTostring ()
{
Return This. GetType (). tostring ();
}

It can be seen that, by default, if you perform the tostring () operation on the type, the full Class Name of the type will be returned. At the same time, it is a virtual method, so that we can rewrite it in our type. We often perform tostring () operations such as string and INT, and actually execute the rewrite method.

When instantiating an object, CLR uses the New Keyword such as classname mycls = new classname ("para"). The execution process is as follows:

1. Calculate the size of the object and all its base classes, including "type Object Pointer" and "synchronous block Index"

2. allocate memory in the managed heap

3. initialize object "type Object Pointer" and "synchronized block Index"
4. Call the constructor and pass parameters. Note the execution sequence of the constructor, and finally execute the constructor of system. object.

5. Return object address (pointer)

Type conversion

CLR emphasizes type security. When the type is converted, it checks the object type. In the following cases, the type is considered safe:

1. The object is converted to its base class. This is a secure implicit conversion.

Test T = new test ();
Object o = T;

2. if the object is converted to the corresponding type, explicit conversion is required. Otherwise, the compilation error occurs.

Test T = new test ();
Object o = T;

Test t2 = (TEST) O;

3. Implicit and secure conversion from small-range data to large-range data types; otherwise, explicit conversion is required.

In addition, if you perform Explicit conversions from the inherited class to the base class, the compilation will fail.

Type conversion using is and

Is operator, checks whether the object is consistent with the given type, returns the Boolean Type true (false). If the object is null, returns false, no error is reported. Typical usage:

Code
If(OIsTest)
{
Test t=(Test)0;
 //Code here
}

This involves two types of checks: The is operator is performed once, And the CLR checks again during Explicit conversions. Performance Loss

A simple and quick way is to use the as operator.

Code

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> test T = O as test;
If (T ! = null )
{< br> /// code here
}< br>

Here, only the type is detected during the as operation. If not, null is returned. In the if statement, it only checks whether the value is null. It is much faster.

Namespace and assembly

There is no one-to-one correspondence between the namespace and assembly.

Abbreviation of namespace using. The full name of the class for the actual operation.

Use aliases to avoid class name conflicts.

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.