C # Advanced (vii) type cast, unboxing, boxing

Source: Internet
Author: User
Tags comparison

One, packing and unpacking

Boxing is the conversion of a value type to a reference type. A unboxing is the conversion of a reference type to a value type.

For example, we are very commonly used. The ToString () method is typically a boxed process.

Again, as in the following example

int i=10;

Object Y= (object) I; This is boxing.

int x= (int) y; This is the unboxing.

Second, the Equality comparison of objects

In C #, there are four more equal methods.

1, ReferenceEquals () The method is a static method that determines whether two references point to the same instance. That is, whether it points to the same memory address, and if so, returns True, otherwise it returns false.

2, the virtual Equals () method. Because it is a virtual method, it can be overridden. This allows it to be used to compare objects or to compare values.

3, Static Equals () method, which is the same as the virtual Equals () method, but with two parameters, and compare it. This method can handle the case where one of the two objects is null, and when one is null, an exception is thrown.

4. Comparison operator = = We'd better consider this comparison as the intermediate option between strict value comparisons and strict reference comparisons. But note that this is the way to compare strings, not references.

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.