[C #]-basics (1)

Source: Internet
Author: User
Tags case statement

1. Glossary

. Net/DOTNET: generally refers to the. NET Framework framework. It is a platform and a technology.

C #: a programming language that can be used to develop applications based on the. NET platform
Java: both a technology and a programming language
Winform: desktop applications developed by. net
ASP. NET: Internet applications developed by. net
WP7:. Net can be used for mobile phone Development
IDE :( Integrated Development) integrated development environment
ASCII (American Standard Code for information interchange, American Standard Code for information interchange)
Msil: Microsoft intermediate language
CLR: Common Language Runtime
Including CLs and CTS (equivalent to words and syntaxes in English). CLR has a service called GC (garbage collection, Garbage Collector)
CLS: Common Language Specification CTS: Common Type System
JIT: instant compiler just in time
Bcl: Base Class Library
The term of the pencil is Il intermediate language.
MFC: Microsoft basic class library Microsoft Foundation Classes
COM: Component Object Model
FCL: Framework
IDE: integrated development environment
Msdn: Encyclopedia Microsoft Developer Network
C/S: Client/Server mode (server) customers need to install dedicated client software such as: QQ
B/S: Only one browser is installed on the Browser/Server mode client.
Both C/S and B/S must be connected.
2. Data Type
Int num = 100; // ------> stores integer data.
Double dounum = 0.12345; // ----> decimal data
* Decimal: Money decimal, which stores money. Pay attention to the format as follows:
Decimal decinum = 24.1234 m; // ----> It also saves decimals, which are of the money type. Pay attention to the m behind the number.
Char CH = 'a'; // ----> character type. Only one character can be saved and cannot be blank.
String STR = "Haha"; // ------> string type, which can be empty
Note: naming rule networking
3. Common escape characters
\: Represents a bar \ ": represents a double quotation mark \ n: represents a line feed \ t: represents a tab, as far as possible to align with the above content

\ B: represents a placeholder. If there is content in front of it, it overwrites the previous content.
@: This symbol can be used to indicate a path without escape.
It has the following usage:
(1) line feed; (2) Add it to the front of the string to invalidate the string; (3) indicate the path.
4. type conversion
(1): automatic type conversion
The operands involved in the operation must first have the same data type, unless the two operands are of compatible type or the target type is greater than the source type.
For example, the int type is compatible with the double type, and the double type> int type can be calculated. Int type can be implicitly converted to double type.
(2): Forced type conversion (display conversion)
Must be compatible, but will lose precision. Example: int num = (INT) 10.5
Remember: int is converted to double by implicit conversion with multiple decimal places. Double is converted to int by display conversion, with a loss of precision.
(3): Other
When the program requires user input, the user input content is received in the string type. If you need to calculate, you can use convert. toint32 (in this example, put the number of the string type to be converted)
For example, int num = convert. toint32 (strnum );
If the user inputs decimal places, double num = convert. todouble (strnum );
5. Arithmetic Operators and compound value assignment operators
(1): You must know about auto-increment and auto-increment: ++ ,--
For example, a ++ first participates in the operation and then adds 1 to itself; ++ A is just the opposite. It adds 1 to itself and then participates in the operation. The usage of a -- and -- A is the same.
We can see from the above: the definition of a unary operator: for an operator like ++, --, which only requires one operand, it is called a unary operator.
(2) compound assignment operators: + =,-=, * =,/=, and % =
Example: int A = 22;
A * = 3; it is equivalent to a = A * 3; the final result output of A is: 66
Operators that require two operands to perform operations, such as the preceding one, are called binary operators.
Priority: The unary operator is higher than the binary operator.
The following is an example:

Int A, B = 6, c = 7;
A = B ++ * -- C; // at this time, the output result of A is: 36
If the above question is changed to: a = ++ B * c --; // at this time, the output result of A is: 49
6. Judgment and loop statements
(1) If-else Statement (where multiple else if statements can be nested)
** Note: The expression in the IF clause must be true or false. For example, in C #, a compilation error usually occurs because "=" does not return bool unless the bool value is being processed.
Syntax:

If (condition 1) {// check whether condition 1 is correct. If so, run the following else if statement else if (condition 2) {Statement 1; // check whether condition 2 is correct. If yes, execute Statement 1} else Statement 2; // If condition 2 is incorrect, execute Statement 2} else {Statement 3; // If condition 1 is incorrect, execute Statement 3}

(2) Switch-case statements (which can contain multiple case statements)
Syntax:

Switch (expression \ variable) {Case value 1: Statement 1; break; case value 2: Statement 2; break;... Default: Statement N; break ;}

Execution Process: when the program is running, first judge "expression \ variable" and compare the value with the case statement one by one. If the value 1 matches, execute Statement 1 and then run break, jump out of the loop, and so on, and execute all the case statements. If none of them match, execute statement n Under default, and then break to jump out of the loop.
(3) While loop (first judgment, then execution; at least one loop body is executed)

Int I = 0; while (condition/expression) --------------------------------- à I <a certain number {cyclic body; I ++; // If I ++ is removed, it will cause an endless loop} // if the condition/expression is true, the loop body will be executed; if not, the loop will jump out and the subsequent code will be executed.

(4) do while loop (execute first and then judge; otherwise, do not execute the loop body once)

Do {loop body;} while (condition );

Execution Process: when the program runs to do, it directly enters the loop body. Then, it goes to the while (condition) line of code to judge. If the condition is true, execute the loop body again. Otherwise, the loop jumps out directly. then execute the following code.
(5) For Loop

For (expression 1; expression 2; expression 3) // If expression 2 or expression 3 is removed, an endless loop {loop body ;} // run expression 1, expression 2, loop body, expression 3, and expression 2 ", execute "loop body", and so on until "expression 2" is not satisfied, jump out of the loop, and the program ends.

##### ---- Break: jump out of the current loop; Continue: Terminate this loop, but do not jump out, and judge the cycle condition. If yes, it enters the next loop, exit the loop when the condition is not met.
7. Ternary expressions and outputs
Syntax: expression 1? Expression 2: expression 3 Example: bool result = 5> 3? True: false, which means to judge whether "result = 5> 3" is true. If it is true, "true" is output; otherwise, "false" is output ".
Output: Int. parse, etc.
8. constants, enumerations, and structures
(1) constant: a fixed constant volume.
Syntax: const type constant name = constant value
For example: const double Pi = 3.14;
Pi = 3.10; // if you run this statement, the error message is displayed: "variables, attributes, or indexer must be on the left of the value assignment number"
(2) enumeration: Generally, enumeration is written outside the namespace and class, but it does not report an error when written in the class.

Enum enumeration name {value 1, value 2,... // The value is fixed and cannot be changed easily. If it is changed, the following enumeration value will be added. Enumeration values are essentially numbers, but are generally not defined as numbers}

#### ---- String and enumeration Conversion
Example: (gender) (enum. parse (typeof (gender), "male"): (Enumeration type to be converted) (enum. parse (typeof (Enumeration type to be converted), string to be converted ))
#### ---- Int and enumeration conversion:

Public Enum jijie {spring, summer, autumn, winter} class program {static void main (string [] ARGs) {console. writeline ("select the season you want, 0-spring, 1-Summer, 2-autumn, 3-winter"); // string STR = console. readline (); // int num = convert. toint32 (STR); // jijie Ji = (jijie) num; // console. writeline ("You selected {0}", JI); // console. readkey (); int num = (INT) jijie. summer; console. writeline ("{0}", num); console. readkey ();}
}

(3) structure (solves the problem of declaring multiple variables of different types at a time)
Syntax: access modifier struct structure name
{
Define structure members
}
Example:

Class program {public struct person {public string name; // a public int age; Public char gender ;} static void main (string [] ARGs) {person zsperson; zsperson. name = "James"; zsperson. age = 22; zsperson. gender = 'male ';}
}

The enumeration and structure can be combined as follows:

Public Enum G {male, female} public struct person {public string name; Public int age; Public g gender;} class program {static void main (string [] ARGs) {person zsperson; zsperson. name = "James"; zsperson. age = 22; zsperson. gender = G. male ;}}

9. Array
<1> four types of declared Arrays:
(1) int [] num1 = new int [10]; // only declare an array with a length of "10"
(2) int [] num2 = new int [3] {11, 24, 55}; // declares an array with a length of 3 and contains the corresponding number of elements
(3) int [] num3 = new int [] {643,456, 34}; // elements are directly added when an array is declared. The length of the array is determined by the number of elements.
(4) int [] num4 = {13,243, 54,67}; // Add an element directly when declaring an array. The default array length is the number of elements.
<2> stored value: stores and assigns values by subscript or index.

Int [] Nums = new int [10]; Nums [0] = 2; Nums = [6] = 5;
Value: it can be set by subscript or index. Console. writeline (Nums [5]);
<3> array sorting array. Sort (Nums); array. Reverse (Nums );
// Check the Bubble sorting method by yourself. It's quite simple. I won't write it here.
10. Simple usage of try... catch

Try {// code with possible errors} catch {// if the code in try has an exception, transfer it to the Code in catch}

Try... catch is intended for programmers to easily modify codes that may encounter exceptions.
** 11. Method
Purpose: reduce the amount of code, put a lot of repeated code together, and call it directly. (A function is a mechanism for reusing a bunch of code)
One method is to complete a function.
(1) Naming rules for methods:
<1> method names start with uppercase, parameter names start with lowercase, and parameter names and variable names must be meaningful.
(2) Syntax:
[Access modifier] Method Name of the static return value type ([parameter list]) ----> [] indicates dispensable. The default value is public.
{
Method body;
}

Public static void show () {console. writeline ("if you want to change the world, then you can change the world ");}

(3) Call Methods
For static method calls, there are two methods: "class name. Method Name ();" or if you are in the same class, you can directly write "method name ();"
(4) static method (as long as you see static in the method, this method is a static method)
(5) If the return value type of a method is void, this method does not return the value;
If this method can assign values to variables, we can say that this method has a return value. variables that accept this Method determine the type of return value of this method.
Ruturn returns the value of the variable and exits the method directly.
(6) method Overloading
Method overloading means that the method name can be the same, but the number of parameters cannot be the same, and the type cannot be the same. Or the number of parameters can be the same, but the type cannot be the same.
12. Class
Syntax: [access modifier] class name
{
Member ;.....
}
Class can contain variable definitions and methods
Class instantiation, with the keyword new. Syntax: class instance name = new class ();
Class member access Syntax: Instance name. Attribute Instance name. Method Name ();
Access modifier: Public Private (internal protected)
(1) attributes
Public modification for fields... private
Attribute definition get, Set
Attribute is to protect the corresponding fields and ensure that the read and assign values of fields meet the requirements.
Attributes can be divided into: read/write, read-only, and write-only (get and set in the attribute represent read and write respectively)
Variables that allow external access must be declared as attributes.
* *** Assign a value to the name attribute, which is equivalent to assigning a value to the field. Classes do not occupy memory, but objects occupy memory.
(2) constructor
The constructor is used to create an object, and the constructor can summarize and initialize the object. Constructors can be overloaded, that is, there are multiple constructors with different parameters.
(3) destructor
1> you cannot define the destructor in the structure, but you can only use the Destructor for the class.
2> A class can have only one destructor.
3> unable to inherit or overload destructor
4> you cannot call the Destructor because it is automatically called.
5> The Destructor has no modifier or parameter.

-- Postscript: this is a small note I made when I systematically studied C #. Most of the content is basic theoretical knowledge for my reference and shared with friends ,@_@

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.