C# Basics - Variables and Expressions

1.1 Basic syntax of C#

    C# is a block-structured language, and all statements are part of a code block. These blocks are delimited by curly braces. A simple example is as follows:

{
<code line1,statement 1>;    
<code line1,statement 2>;   
<code line1,statement 3>;
}    

   In C#, a commonly used statement is a comment. Comments are to add descriptive text to the code to increase the readability of the code.

// This is a comment (single line comment)

/* Thank you for Reading this note (paragraph note) */

1.2 Variables

Variables are related to the storage of data. To use variables, you must declare them, that is, assign a type and a name to the variable. Once variables are declared, they can be used as storage units for the declared variable type.

<type> <name>;

Integer type
sbyte System.Sbyte Integer between -128 and 127 (2 to the seventh power)
byte System.byte Integer between 0-255 (2 to the 8th power)
short System.Int16 Integer between -32768 and 32767
ushort System.Uint16 an integer between 0 and 65315
int System.Int32 -2^31---2^31-1
uint Syetem.Uint32 2 ^ 32-1 (correction)
long System.Int64 2^63
ulong System.Uint64 2^64

  1.2.1 Naming of variables

The first character of the variable name must be a letter, underscore or @

Subsequent letters can be letters underscores, numbers.

2.1 Expressions

Operators mainly include:

  • arithmetic operators
  • assignment operator
  • relational operator
  • Logical Operators
  • conditional operation
  • bitwise operator
  • String operators.

 

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

The expressions mainly include:

  • arithmetic expressions
  • assignment expression
  • conditional expression
  • boolean expressions etc.

 

Arithmetic Operators and Expressions

There are five operators provided by C#: "+" addition operator, "-" subtraction operator, "*" multiplication operator, "/" division operator, and "%" modulo operator. Arithmetic operators are preceded by multiplication and division followed by addition and subtraction.

 

Assignment Operators and Expressions

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

C# allows continuous assignment of variables, such as a=b=c.

The associativity of the assignment operator is right-to-left, so a=b=c is equivalent to a=(b=c).

 

Relational Operators and Expressions

A relational operator is actually a "judgment" symbol whose result is "true" or "false".

 

Logical Operators and Expressions

C# includes three logical operators: "and (&&)", "or (||)", "not (!)".

Among them, the "!" operator is a unary operator, which means that it has only one operand.

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

 

bitwise operators

Bitwise operators can be divided into shift operators and logical bitwise operators. Any information in a computer is stored in binary form, and bit operators are operators that operate on data in binary.

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325020763&siteId=291194637