C # advanced programming learning notes (2) operator and type conversion

Source: Internet
Author: User

5. Operator and type conversion

1. Operators
C # Four of the supported operators (sizeof, *,->, and &) can only be used for security reasons.Code(Bypass the C # type security check ).
Ternary operators (? :)
Checked and unchecked operators: CLR checks overflow. If overflow occurs, an exception is thrown.
Is OPERATOR: checks whether the object is compatible with a specific type (indicating that the object belongs to this type or is derived from this type ).
As OPERATOR: used to convert the display type of the reference type.
Sizeof OPERATOR: determines the length (in bytes) required for the stack value type ).
Typeof OPERATOR: returns a system. Type object of a specific type. (Usually used to reflect the information of the dynamic search object)
Optional types and operators:ProgramCan be empty type, must consider the impact of the null value in use with various operators. Generally, when the null type is used together with a single or binary operator, if one or both of the operands are null, the result is null.
Null join operator (??) : Provides a shortcut for processing the possibility of null values in a table when the table can be empty or referenced.
Operator priority:
Group Operators
Primary operators (), [], X ++, X --, new, typeof, sizeof, checked, unchecked
Unary operators +,-, and ,-,! ,~ , ++ X, -- X, and data type conversion
Multiplication and division operators *,/, and %
Addition and subtraction operators + ,-
Shift Operator <>
Relational operators <> <=> = is
Comparison operator =! =
Bitwise AND operator &
By XOR operator |
By or operator ^
Boolean and operator &&
Boolean OR operator |
Ternary operator? :
Value assignment operator = + =-= * =/= % = <<=>>>>>=

2. type security
Type conversion: implicit type conversion, explicit type conversion
Packing and unpacking: boxing is a conversion from value type to reference type, and unpacking is a conversion from reference type to value type.
3. Equal comparison of Objects
Comparison of equal reference types: referenceequals () method, virtual equals () method, static equals () method, comparison operator =
Equality comparison of value types: equals () performs an appropriate equality test on the value type. If the value type contains the reference type of the field, you need to override equals (), in order to provide appropriate semantics for these fields, because the default versions of equals only compare their addresses.
4. Operator Overloading
The overload operator allows you to generate code that is more readable and intuitive, including:
In mathematics, almost all mathematical objects include coordinates, vectors, matrices, tensor and functions. If you write a program to perform mathematical or physical modeling, the class will definitely represent these objects.
When a graphics program calculates the position on the screen, it also uses mathematical or related coordinate objects.
Class that represents a large amount of money (for example, in financial procedures)
Word processing or text analysis programs also have classes that represent statements and clauses. You can use operators to connect statements together (this is a complicated version of string connection)

C # requires that all Operator Overloading be declared as public and static, which means they are associated with their classes or structures rather than instances, therefore, the Code body overloaded by operators cannot access non-static class members or the This identifier.
Add more operators
C # The = operator is not allowed to be reloaded. However, if the + operator is reloaded, the compiler automatically uses the + operator to perform the + = Operator operation. The-=, & =, * =,/= assignment operators also follow this rule.
Comparison Operators
Overload = and! =, The equals () and gethashcode () methods inherited from system. object should also be reloaded. Otherwise, a compilation warning will be generated. The reason is that the equals () method should execute the same equal logic as the = Operator.

5. User-defined data type conversion
Data type conversion between classes: two restrictions:
If a class inherits another class directly or indirectly, you cannot convert the two classes (these types already exist ).
Data type conversion must be defined within the source or target data type definition.

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.