C # data types, operators

Source: Internet
Author: User

Data type:
Integral type: int short long byte
Decimal: Double Float Decimal
Boolean: BOOL
Character: Char

Define variables:
Data type variable name [= value];
Naming rules for variable names:
1. Composition of characters: letters, numbers, underscores, @, kanji
2. First character: Only with letters, Chinese characters, underline, @
3. Cannot be weighed with keywords:

Constant:
Const data type constant name = value;
Constants must be assigned when they are defined.
Once a constant is defined, it is not possible to modify its value with a proxy. Constants can be placed on the left side of the equals sign only when defined.

Data conversion:
When computing, you must use the same type of data for the operation.
If the data is not the same type, a type conversion is required.
Classification of type conversions:
1. Automatic conversion: As long as there is no possibility of data loss, the automatic turn.
Byte->short->int->long->float->double
2. Cast: All cases where data loss may occur, a forced transfer is required.
Add parentheses to the left of the converted data: (the type of data to be converted to)
Use CONVERT.TOXXXX (the data to be converted)
Example:
int d = (int) 3.14;
int d = Convert.ToInt32 (3.14);
float f = (float) 3.14;
float f = convert.tosingle (3.14);

You must use CONVERT.TOXXX (string) If it is a string conversion base data.


Operator:
First, arithmetic operation:
+ - * / % ++ --
Attention:
1. When doing a division, if the two operands are the same integer, the result of the operation is also an integer and no decimals appear.
2. Above these operations, if the two operands are of a different type, the type conversion will be performed automatically at the time of operation.

What are the residual uses?
1. Can the judgment be divisible?
2. The number used to turn a number into a range.

Self-increment and decrement operations:
Grammar:
Variable name + +; + + variable name;
int a = 4;
a++; ++a;

Significance:
a++ ++a <==> a = a+1; <==> 1. Take out the value of a. 2. Add the value of a to 1. 3. Assign the result of the addition to a.
Why is 5++ not correct?
const int b = 5;
b++; Why isn't it right?

What is the difference between ++a and a++?
1. If there is only one + + or--operation in this line, there is no other operation, the first + + post + + effect is the same.
2. If this line of statements, in addition to + +-, there are other operations.
First + +, executed first. After + +, the last execution.
For example:
int a = 5;
int b;
b = a++;//equivalent to B = A; A = a+1;
Console.WriteLine (a);//a=6
Console.WriteLine (b);//b=5
b = ++a;//equivalent to a = A+1;b=a;
Console.WriteLine (a);//a=6
Console.WriteLine (b);//b=6

Two, relational operators: 6
= = = > >= < <=
All relational operators, and the result of the operation is a bool type.

int a = 5;
int B = 6;
Console.WriteLine (A = = B);//false
Console.WriteLine (A! = b);//true
Console.WriteLine (a > B);//false
Console.WriteLine (a < b);//true
Console.WriteLine (a >= b);//false
Console.WriteLine (a <= b);//true

Note: do not write = =

Third, logical operators
&& | | !

int a = 5;
int B = 6;
A>6 && b<=10//result is False

18<a<35 ==> a>18 && a<35
a<18 or a>35 ==> a<18 | | A>35

&&--only two conditions are established, the result is true; if either one is not true, or two is incorrect, the result is false.
|| --unless two are not established, the result is not false; As long as there is a set up, the result is true.
! --Take the inverse operation.

int a = 16;
BOOL B = a>18 && a<35; b = = False
BOOL C =! (a>18 && a<35); c = = True

Iv. Other operations
=--Assignment operation, assigns the right value to the left side. So it can only be a variable on the left, not a constant or an expression.
+ = = *=/=%=---compound operator.
int a = 5;
A + = 10; <==> a = a+10; 1. Execute a+10. 2. Assign the result of the addition to A;
?:--The condition operator.
Logical (Relational) Formula Two: Equation three;
int a = 10
int b = 5;
BOOL C = a>b?a:b;

Homework:
1. Find the maximum value from the keyboard by entering three numbers.


2. Convert the 24-hour time into a 12-hour system. --what time is it, old Wolf

C # data types, operators

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.