C # Basic syntax

xiaoxiao2021-03-17  204

This is my study notes, the latest version is released in wiki.deepcast.net. First, the comment statement / ** // * Comment statement is included in the "backslash *" and "* backslash", or two backslash and wrap, or three backslash and wrap Among them (can be automatically identified as file comments to be subsequently identified as files) To pay attention to /, the continuation of the next line will comment on the next line. * / Static void main (string [] args) {// statement block is included in {} int myinterger; // statement; end string mystring; / ** //// ignore blank characters (space / Enter / Tab ) MyInterger = 17;} // Be careful to prevent annotation to nest errors, available # ix, # ENDIF pre-processes. Second, data type 1, basic / built-in user-defined A, built-in type: CS uses the data type of the .NET Framework library Sbyte = system.sbyte, ... Type bytes Interpretation BYTE 1 No Symbol byte SBYTE 1 Has Symbol-byte Short 2 Has Sign Short Texture Type USHORT 2 No Symbol Short Manage INT 4 Has Symbol Integer UINT 4 No Symbol Integer LONG 8 Has Symbol Long Ulong 8 No Symbol Long Integer FLOAT 4 floating point Double 8 double precision Decimal 8 fixed precision String Unicode string type Char Unicode character type BOOL true and false Boolean // only accept true and false two values. Do not accept any integer type. B. User-defined types include: Class Type (Class) Structure Type (Intert) Interface Type (Interface) 2, Value Types and Reference Type (Reference Types) A, Value Type: Memory Square In the Stack. They include: all basic or built-in types (excluding string types), structural types, enumeration types (ENUM TYPE) B, reference type: memory nonlinear allocation is in the heap, when they are no longer used by garbage collectors Automatically release memory (C with delete). They use the New operator to create. The reference type includes: class type, interface type, set type type, string type, enumeration type structure, is suitable for quick access and has a small number of members. If there is more, you should create a class to implement him. 3, data type conversion implicit conversion: transition from low accuracy to high precision, so it is impossible to convert to char; other integer can not be converted into enumeration.

Explicit conversion static void Main (string [] args) {short shortResult, shortVal = 4; int integerVal = 67; long longResult; float floatVal = 10.5F; double doubleResult, doubleVal = 99.999; string stringResult, stringVal = "17"; Bool boolval = true; console.writeline ("Variable Conversion Examples / N Data Type Transformation Example / N"); DoubleResult = Floatval * Shortval; console.writeline ("Implicit, -> Double: {0} * {1} -> {2} ", floatval, shortval, doubleresult); shortResult = (" implicit, -> short: {0} -> {1} ", floatval, shortresult); stringResult = Convert.TOSTRING (BOOLVAL) Convert.TOString (DoubleVal); Console.Writeline ("Explicit, -> String: /" {0} / " /" {1} / "-> {2}", Bool, DoubleVal, StringResult LongResult = INTEGERVAL Convert.Toint 64 (Stringval); console.writeline ("Mixed, -> long {0} {1} -> {2}", Integerval, Stringv Al, longresult); Before retaining keywords, keeping with other languages) (2), declared variables: Variable type variable name variable assignment: Variable = value C # variable must be initialized before being accessed; otherwise it will be reported when compiling.

Therefore, it is impossible to access an uninited variable (such as an unstertified pointer, an expression of an array boundary). The best habit before use is to declare and initialize. (3) There is no global variable or global function in C #, the operation of the overall mode is achieved by static functions and static variables. INT i; string text; // does not initialize for (i = 0; i <10; i ) ... {text = "line" convert.toString (i); // Not initialized in the cycle, The value will be lost when you exit the cycle, and then it is wrong. Console.writeLine ("{0}, text);} console.writeline (" Last Txet Output in loop: {0}, text); // error. The correction method is to initialize the recycling: String text = ""; (4) Naming convention: Simple use of Camelcase, complex use of seven types of PASCALCASE (5) variables: Class A ... {public static int x; / / Static variable, existed from the class load until the program ends. INT Y; / / non-static variable, or instance variable, created from class instance to instance space release. / ** // * v [0] is an array element, A is a value parameter, B is a reference parameter, c is the output parameter * / void f (int [] V, INT A, REF INT B, OUT INT C). .. {INT i = 1; // Local variable, will not be initialized C = A B ; //}} 2, enumerate Enum enumeration name: Enumeration value type (default is int, default assignment 0, 1 , 2 ...) {enumeration value 1 = ..., enumeration value 2 = ..., enumeration value 3, // does not assign a value, the default is the last defined value 1 ... } Enumeration name variable name = enumeration name. Enumeration value Namespace ch05EX02 ... {enum orientation: byte ... {north = 1, South = 2, EAST = 3, West = 4} / ** /// / Class1's Desciptionclass Class1 ... {static void Main (string [] args) ... {byte directionByte; string directionString; orientation myDirection = orientation.north; Console.WriteLine ( "mydirection = {0}", myDirection); directionByte = (Byte) MyDirection; // Because ENUM existed Byte, it is theoretically in turn to convert, but logically is not necessarily correct.

MyDirection = (orientation) MyByte; Directionstring = Convert.TOString (myDirection); // Equity command is Directionstring = myDirection.toString (); // Because it is not only to transfer the enumerated variable to the String variable, it cannot be used String (myDirection); // The reverse conversion command is orientation myDirection = (orientation) enum.parse (TypeOf (orientation), mystring); but because enum does not necessarily save String, it may be wrong. If mystring is assigned to NORTH, it cannot be mapped to North, an error. Console.writeline ("Byte Equivalent = {0}", DirectionByte); Console.writeline ("String Equivalent = {0}", Directionstring);}}} 3, Structure Struct Structure Name: {Access Method 1 Variable Type 1 Variable Name 1; // Access Method PUBLIC / PRIVATE Access Mode 2 Variable Type 2 Variable Name 2; ...} Structure Structure Variable Name; Structural Variable Name. Enumeration Value = ...; 4, One - dimensional array variable type [ ] Array name = new variable type [element number] ... {element 0, element 1, element 2 ...} // element number must be integer or integer constant, and must be numbered with the back elements The same, otherwise it will be wrong. The number of new declarations of the element and the back element column values ​​can only be selected and the array can be declared and initialized.

Traversing method A, for loop to .lengthfor (i = 0, i > Comparative Operator: Small than less than or equal to <= greater than or equal to> = comparison operator: ==,! = Logical operator: & logical operator: ^ logical operator: | logical operation符: && logical operator: || Compare operator: equal to = * = / =

% = = - = << = >> = & = ^ = | = Thermalfix and - 6, the namespace using system; // system is the .NET Framework application root namespace, then can be in the global Name Space Reference The code in System. NameSpace Space Name ... {Using space name 2. Code 2; // can directly reference code 2 code 2 code 1; Namespace Space Name 2 ... {code 2;}} seven, conditional statement A, IF statement: Universal judgment if (condition 1) code 1; ELSE code 2; if (condition 1) {} else {} b, switch statement: Used to determine Switch (Case Result 1) : Code 1; Break; Case Result 2: Code 2; Break; Case Result 3: Code 3; Goto Case Result 2; // At this time, Case .. is equivalent to a label case result 4: Code 4; Return; Case result 5 : CASE Result 6: Case Result 7: Code 567; // Just satisfying one of the three Cases. Break; ... default: code; Break;} C, three yuan operation statement: (condition)? true results: FALSE results are often used for simple assignment statements: String mystring = (Myinteger <10)? "Less Than 10": " Great tour or equal to "; or for simple formatting text statements: console.writeline (" i am {0} year {1} old. ", Myinteger, myinteger == 1?": "S"); eight , Circulating statements: a, do ... while: Circulation when the condition is TRUE. Do ... {...} while (condition); // semicolon is indispensable B, while ...: When the condition is TRUE, it is loop. While (Condition) ... {...} C, for ...: Use the counter loop. FOR (variable; condition; operation) / / can declare variables at this time, but the scope is limited to the loop.

... {... BREAK; // Take out the entire loop return; Continue; // Above the current loop, continue the next loop GOTO tag; // Do not prohibit the accession of the Goto from the loop to jump into the loop internal} nine, functions a, functions Definition: Static Return Value Type / Void Function Name (Parameter Type 1 Parameter 1, Parameter Type 2 Parameters 2, ...) ... {... return return value; // return must be processed before the function, cannot Skip // Return; // uses Return that does not return value with the VOID to trouble the function. } B, parameter arrays: static int sumvals (params int [] value) ... {int sum = 0; Foreach (int valin value) ... {SUM = Val;} Return Sum;} static void main (String [] args) ... {int sum = sumvals (1, 5, 2, 9, 8); console.writeLine ("Summed Values ​​= {0}", SUM);} C, Value Transfer Parameters / Reference Parameters / OUT output parameter static void showdouble // Reference delivery parameter ... {Val * = 2; console.writelie ("Val doubled = {0}", val);} static void showdouble2 (int val) // Value Transfer Parameters ... {VAL * = 2; Console.Writeline ("Val Doubled = {0}", VAL);} static void showdouble3 (int valid showdouble3 (int valid showdou) // OUT output parameter ... {Val * = 2; ValIndex ; console.writeline ("Val doubled = {0}", val);} int mynmuber = 5; showdouble (ref myNumber); // Reference Transfer parameters will change the MyNumber value, so ask MYNumber Must be a variable that is constant or uninited. Showdouble2 (MyNumber); // Value Transfer Parameters, does not change the MyNumber value int valindex; // OUT output parameter, no initialization, initialization will lose value when the function starts. Showdouble3 (MyNumber, Out ValIndex); D, global variable static / const variable name // constinity is read-only when global variables are in the same name as the local variable, the local variable is preferred, and the global variable is preferred to use similar class1.mystring variable.

E, main () function; static void main () static void main (string [] args) // args is a function command line parameter static int main () // returns an int value static int main (String) [] Args) // Returns a INT value f in the function termination state, the function g, the overload of the same name function: the same name, the signature, which function is automatically recognized which function h, delegate: used to store the reference Function to flexibly call a function of 10, object-oriented basic eleven, class 1, class definition Class class name ... {// class member} INTERNAL / PUBLICSEALED / Abstract Ten, Interface IMYINTERFACE ... {// interface Members} TRACKBACK: http://tb.blog.9cbs.net/trackback.aspx?postid=455325

转载请注明原文地址:https://www.9cbs.com/read-129687.html

New Post(0)