Clrviacsharp Study Notes

Source: Internet
Author: User

I. Basic Types
1. type conversion:
A derived class is converted to a base class without forced conversion. The base class must be forcibly converted to a derived class.
Object o = new employee ();
Employee E = (employee) O;
Non-Derived classes. CLR prohibits conversion and throws system. invalidcastexception.
2. Is and as operator Transformation
Similarities:
Is and as check whether an object is compatible with the specified type.
Never throw an exception
Differences
Is returns true or false
As returns a non-null reference to the same object. If not compatible, null is returned.

Object o = new object ();
Bool b1 = (O is object); // B1 is true.
Bool b2 = (O is employee); // B1 is false.

If (O is employee ){
Employee E = (employee) O;
// Use e in the remaining part of the IF statement
}

 

InCodeFor the first time, check whether o is compatible with the employee type. In the if statement, CLR checks again whether o references an employee.
The as operator simplifies the code and improves performance.
Employee E = O as employee;
If (E! = NULL ){
// Use e in the IF statement
// If the object cannot be transformed, the result is null. If you attempt to directly use the final reference, the system. nullreferenceexception will be thrown.
}

 

Object o = new object ();
Employee E = O as employee; // The Transformation Operation will fail without throwing an exception. The value of E is set to null.

E. tostring (); // throw system. nullreferenceexception

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.