C # Operator

Source: Internet
Author: User

1. Conditional operators (? :), Also known as the ternary (object) operator, is a simplified form of the if... else structure and can be nested for use. [Csharp] int x = 1; string s = x + ""; s + = (x = 1? "Man": "men"); Console. writeLine (s); // output 1man 2, checked, and unchecked [csharp] byte B = 255; {B ++;} Console. writeLine (B. toString (); // the output value is 0, but the byte value can only contain 0-values. Therefore, B overflows after ++. Therefore, if a code block is marked as checked, the CLR performs an overflow check. If a code block overflows, an OverflowException exception is thrown. [Csharp] byte B = 255; checked {B ++;} Console. writeLine (B. toString (); // throw an OverflowException exception. overflow caused by arithmetic operations. To disable the overflow check, mark it as unchecked: [csharp] byte B = 255; unchecked {B ++;} Console. writeLine (B. toString (); // outputs 0 without throwing an exception. 3. The is operator can check whether the object is compatible with a specific type. "Compatible" indicates that the object belongs to this type or is derived from this type. [Csharp] string I = "hello I... "; if (I is object) {Console. writeLine ("I is an object... "); // executed this sentence} 4. The as operator is used to execute the explicit type conversion of the reference type (string is the reference type ). If the type to be converted is compatible with the specified type, the conversion is successful. If the type is incompatible, The as operator returns Null. [Csharp] string I = "hello I... "; if (I is object) {object obj = I as object; // Console for explicit type conversion. writeLine (obj is string? "Obj is string... ":" obj is not string... "); // output obj is string ...} 5. the sizeof operator can determine the length (in bytes) required for the stack Value Type: [csharp] int byteSize = sizeof (byte ); // output 1 int charSize = sizeof (char); // output 2 int uintSize = sizeof (uint); // output 4 int intSize = sizeof (int ); // output 4 6. the typeof operator is often used together with the GetType () method to reflect the attributes and methods of the class. [Csharp] Type intType = typeof (int); System. reflection. methodInfo [] methodInfo = intType. getMethods (); methodInfo. toList (). forEach (x => Console. writeLine (x. name); // reflect the method Name of the int type. 7. null type and operator. if one or both of the operands are null, the result is null, for example: [csharp] int? A = null; int? B = a + 4; // B = null int? C = a * 5; // c = null, but when the comparison can be null, if one of the operands is null, the comparison result is false. However, because a condition is false, the opposite of the condition is true. For example, [csharp] int? A = null; int? B =-5; if (a> = B) Console. writeLine ("a> = B"); else Console. writeLine ("a <B"); // This sentence is output. 8. null merge operators such as: [csharp] int? A = null; // Add a question mark to assign a null Int B to the int type; B = ?? 1; [csharp] Console. WriteLine (B); // output 1 a = 3; B = ?? 10; Console. WriteLine (B); // output 10

Related Article

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.