C # Variables and expressions

Source: Internet
Author: User
Tags bitwise operators true true

Name of the variable:

The first character must be a letter, underscore, or @;

The subsequent characters can be letters, underscores, or numbers.

Note: Case sensitive.

Types of variables:

    • Numeric type

1. Integer type

BYTE, short, int, long

SByte, ushort, uint, Unlong

2. Floating-point number type

float, double, decimal

    • Simple Type

char, BOOL, string

Number of bytes in the account:

Variable declaration:

int myinteger=17;
String mystring= "Hello world!";
Console.WriteLine ("{0} {1}", Myinteger, myString);

Escape characters:

The operators mainly include:

    • Arithmetic operators
    • Assignment operators
    • Relational operators
    • logical operators
    • Conditional operations
    • Bitwise manipulation Operators
    • The string operator.

An expression is a combination of operators and operands, such as a*b+1-c.

The expressions mainly include:

    • An arithmetic expression
    • An assignment expression
    • Conditional expressions
    • Boolean expressions, etc.

Arithmetic operators and expressions

C # provides five operators: the "+" addition operator, the "-" subtraction operator, the "*" multiplication operator, the "/" division operator, and the "%" modulo operator. The precedence of arithmetic operators is calculated in the order in which they are multiplication and then added and reduced.

Assignment operators and expressions

Assigning a value is giving a new value to a variable. The assignment operators in C # are: =, + =,-=, *,/=,%=, &=, |=, >>=, <<=, ^=.

In C #, variables are allowed to be continuously assigned, such as A=b=c.

The binding of the assignment operator is from right to left combined, so a=b=c is equivalent to A= (b=c).

relational operators and expressions

A relational operator is actually a "judge" symbol with the result of true (true) or False (false).

logical operators and expressions

C # consists of three logical operators: "With (&&)", "or (| | ) "," Non-(! )”。

Which, "!" The operator is a single-mesh operator, meaning it has only one operand.

Their operands are expressions of a Boolean or Boolean value, and the operation result is a Boolean value of "true" or "false".

Bitwise operators

The bitwise operators can be divided into shift operators and logical bits operators. Any information in the computer is stored in binary form, and the bitwise operator is the operator that operates on the data in binary order.

The operators in C # include ">>", "<<", "^", "&", "|", "~".

Attention:

 for (&&,| |), the object of the operation is a logical value, which is true/false  && equivalent to Chinese and, | |  True  && true = && False = false  && True = && False =
     
       True 
      | | True = | | False = | | True = | | False = False 
 for (&,|), the object of the operation is a bit, that is, 1/0   1  & 1  = 1  1  & 0  = 0  0  & 1  = 0  0  & 0  = 0  1  | 1  = 1  1  | 0  = 1  0  | 1  = 1  0 | 0 = 0< /span> 

Self-increment and decrement operators

j=i++: First assigns I to the J;i plus 1;

J=++i:i first add 1, then assign the value to J;

Conditional operators

Syntax: variable name = (condition)? value 1: Value 2.

Run: If the condition is true, then the conditional expression evaluates to a, otherwise the value is B.

Comma operator

The general form of a comma expression is: expression 1, Expression 2

The order of execution of the comma expression is: first solve expression 1, then solve Expression 2. The value of the entire comma expression is the value of expression 2. For example, the expression "1+2,3+4", the value of the comma expression is 7.

Operator precedence level

Priority level

Operator

From high to Low

+ + 、--(prefix); +,-(unary)

*、/、%

+,-

=, *=,/=,%=, + =,-=

+ + 、--(suffix)

C # Variables 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.