C # Operators and expressions

Source: Internet
Author: User

#保留了C + + All operators, where the pointer operator (* and,) and the reference operator (&) require an unsafe context. C # rejects the scope discrimination operator (::) and changes to the single-point operator (.). We no longer elaborate on those reserved C + + operators, here are some of the special meanings that C # introduces: As,is,new, Typeof,sizeof,stackalloc.

The as operator is used to perform conversions between compatible types, and when the conversion fails, the AS operator results in NULL. The IS operator is used to check whether the run-time type of an object is compatible with a given type, and if the expression is non-null and can be converted to the specified type, the IS operator evaluates to true, otherwise false. The AS and is operators are designed based on the same type identification and conversion, and they have similar applications. Actually expression as type is equivalent to expression is type? (type) expression: (type) null.

As new for the operator is used to create objects on the heap and call constructors, it is worth noting that value type objects (such as structs) are created on the stack, whereas reference type objects, such as classes, are created on the heap. New is also used for modifiers that hide inherited members of base class members. To hide an inherited member, declare the member in the derived class with the same name and modify it with the new modifier. The typeof operator is used to obtain a type of System.Type object, and we will elaborate on it in the "tenth feature and map" combined with Microsoft.NET's type system. The sizeof operator is used to obtain the size (in bytes) of a value type (not for reference types). Stackalloc is used to allocate memory blocks on the stack and is only valid in the initializer for local variables, similar to the _alloca of A/C + + language. Both sizeof and STATCKALLOC require an unsafe context because of the direct manipulation of the memory involved.

Some of the operators in C # can be overloaded as they do in C + +. Operator overloading enables custom types (classes or structs) to easily express certain commonly used operations with simple operators.

The combination of a series of operators and operands for the completion of a computed result is called an expression. Like C + +, C # expressions can be divided into assignment expressions and Boolean expressions, and C # does not introduce a new form of expression, which we will not repeat.

C # Operators and expressions

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.