C # Summary

Source: Internet
Author: User

Data types--variables and constants--operators and expressions--statements (if,for)--arrays--functions--structural bodies

First, the data type:
(i) Built-in type
Integral type (int short long byte uint ushort ulong sbyte), floating point (double float decimal), Boolean (bool), character (char)

There is a ToString ("formatted string") for integral and floating-point types:
#--any number. Some words are displayed, not on the display.
0--must have a number, if not, fill 0.
.--Decimal point
,—— thousands separated.

(ii) commonly used classes
Math DateTime String
Math:
Math.ceiling (Double): the smallest integer greater than the current decimal.
Math.floor (Double): The largest integer less than the current decimal.
Math.Round (double): Rounding
Datetime:
Year,month,day,hour,minute,second,millisecond,dayofweek,dayofyear
AddYears (n), AddMonths (), AddDays () .....
ToString ("formatted string"): Format display.
yyyy,yy--year. mm,m--month. dd,d--days. When hh,h--. mm,m--points. ss,s--seconds. ms--ms

(iii) Custom type
struct

Second, variable and constant:
(a) A variable is a container for loading data. --u Plate
Defined:
Data type variable name [= value], variable name [= value],....;
int a,b;int a = 5,b;
Naming rules for variables:
1. Variable names can only be composed of letters, numbers, underscores
2. Only letters, underscores start
3. Cannot repeat with keyword.
Assignment value:
Variable name = value;-Note: The variable type is consistent with the value type on the right side of the equals sign. Inconsistency requires a type conversion.
Type conversions:
1. Automatic conversion: Generally automatic conversion, as long as there is no possibility of data loss, the calculation will be automatically converted. For example: double A = 3+5.0;
2. Cast: As long as there is the possibility of data loss, the computer will not be automatic conversion, need to manually forced conversion.
Convert.toxxx (); Convert.ToInt32 ();
Double A = 3.14;
int b = (int) A;
Value: Write the variable name directly.
(b) Constants: Constants are also containers for data, but constants cannot be placed on the left side of the order during operation. --Disposable discs
Category: Literal constants, symbolic constants.
Definition: const int PI = 3.14;
Note: Constants must be assigned when they are defined.
Value: Use the constant value directly.

Third, Operator:
Arithmetic, relationship, logic, other
(i) Arithmetic--7
+ - * / % ++ --
Integer, in addition to integers or integers.
(ii) Relationship--6
= = = > >= < <=
(iii) Logical--3
&& | | !
(iv) Other
1. Composite operator: + = = *=/=%=
2. Assignment: =
3. Conditional operator: expression 1? expression 2: Expression 3

Iv. statements: Order, branching, looping
(a) Branch--if
if (an expression)
{
}

if (an expression)
{
}
Else
{
}

if (an expression)
{
}
else if (expression)
{
}
...
Else
{
}

if (an expression)
{
if (an expression)
{
}
Else
{
}
}
Else
{
...
}

Example:
1. Judging leap years, common year
2. The case of the root of a unary two-time equation
3. Weight and height of men and women
4. Determine if the date is correct.


(b) Circulation
for (initial condition; cyclic condition; variable change)
{
Loop body
}
Four elements of a loop:

Loop nesting: Prints an asterisk.

Two kinds of problems: Iterative method and exhaustive method.

Iterative method: The solution is solved by cyclic iteration according to a certain rule.
Ask for the number of 100 and the factorial.
1.5 Children seeking age
2. Placing grain on the board
3. Origami with the height of Everest.
4. Drop the ball problem.
5. Monkeys Eat Peaches
6. Rabbits give birth to rabbits.

Exhaustive method: Take all the circumstances to go through, according to the conditions of screening.
The number of 7 related to 100 is calculated.
1. Buy something.
2. Hundred Chicken Hundred Money, hundred Ma Baishi.
3. Scout
4. Ask for an equation.


Five, array:
Thinking: Solve a large number of similar data storage and operation problems.
Features: continuous, same class of data.
Category: One-dimensional arrays, two-dimensional arrays, multidimensional arrays.

One-dimensional arrays:
Defined:
data type [] Array name = new data type [length of array] [{initialize}];
Assignment value:
Array name [subscript] = value;
Can be combined with loops.
Value:
Array name [subscript];
Can be combined with loops.

Example:
1. Player rating
2. Election of the Monitor
3.36 Select 7
3. Green Song race.

Two-dimensional arrays:
Defined:
data type [,] array name = new array type [number of rows, number of columns] [{initialize}];
Assignment value:
Array name [row subscript, column subscript] = value;
Value:
Array name [row subscript, column subscript];
Example:
1. Student performance.
2. Push the box.

Two applications: Binary lookup, bubble sort.
Binary method to find the idea: The premise is that the array is ordered, each time to find the middle of the value of the comparison, whether the condition is thrown half.
Use the maximum subscript max, min subscript min, mid-value subscript mid, and control the range of lookups. Mid = (max+min)/2; max = mid+1; min = mid-1;
If you keep checking the Min>max, it's over, and the instructions are not found.

Bubble sort idea: The adjacent two numbers are sequentially contrasted and interchanged.
Two-layer loop, outer loop number, inner loop number of times per trip.
Number of trips: n-1
Number of times: n-i
for (int i=1;i<=n-1;i++)
{
for (int j=1;j<=n-i;j++)
{
if (A[j] > A[j-1])
{
Swap.
}
}
}

Six, Function:
What is a function: the ability to complete a separate function module can be called a function.
Why use functions: Clear structure, Division of labor Development, code reuse.
Four elements: function name, formal parameter, return type, function body.
Definition syntax:
return type function name (formal parameter list)
{
function body
}
Call:
Function name (argument list);
Data type variable name = function name (argument list);

The problem of the function's transmitting value and the address.
1. Built-in type, date time is the default value. --ref
2. Array, the string is transmitted by default.

The return value of the function. --return the value or variable, and the type to be persisted after return is the same as the return type of the function.

Recursion. Himself to tune himself.
Grammatical thought:
return type function name (parameter)
{
1. End the judgment of recursion.
2. Recursive operation: function name (parameter);
}

VII. structure of the body:
Why use structs? A composite type that you define yourself to better mimic the various objects in your life.

Defined
struct Structure Body name
{
Public type sub-variable name;
Public type sub-variable name;
....
}

Use:
struct body name struct variable = new struct name ();
struct variable. Sub variable = value;
struct variables-sub variables;

struct array:
struct type [] array name = new struct type [length];
Array name [subscript]. Child variables

How to use loops to manipulate an array of structs.

Example: Student performance statistics. PvP Games.

C # Summary

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.