Basic concepts ---- Beginning Visual C #

Source: Internet
Author: User

Basic concepts ---- Beginning Visual C #
Variable annotation method: // annotation here and/* annotation here */Integer variable Type: Type Alias for Allowed Valuessbyte System. SByte Integer between-2 ^ 7 and 2 ^ 7-1byte System. byte Integer between 0 and 2 ^ 8-1short System. int16 Integer between-2 ^ 15 and 2 ^ 15-1ushort System. UInt16 Integer between 0 and 2 ^ 16-1int System. int32 Integer between-2 ^ 31 and 2 ^ 31-1uint System. UInt32 Integer between 0 and 2 ^ 32-1long System. int64 Integer between-2 ^ 63 and 2 ^ 63-1ulong System. UInt64 Integer between 0 and 2 ^ 64-1 floating point: Type Alias for Approx Min Value Approx Max Valuefloat System. single 1.5x10-45 3.4x1038double System. double 5.0x10-324 1.7x10308decimal System. decimal 1.0x10-28 7.9x1028 other simple types: Type Alias for Allowed Valueschar System. char Single Unicode char, between 0 and 65535 bool System. boolean true or falsestring System. string a sequence of characters Batch name: for simple variables, you can use the camelCase format, such as firstName. For some advanced variables, you can use the PascalCase format, such as LastName. This is recommended by Microsoft. Literal constant: true, false, 100,100 U, 100L, 100UL, 1.5F, 1.5, 1.5 M, 'A', "hello" verbatim, verbatim constant: "C: \ Temp \ mydir \ myfile.doc "is equivalent to @" C: \ Temp \ mydir \ myfile.doc ". In addition, you can enter a string across lines, such: @ "first linesecond linethird line" there is a requirement in many languages for variable usage, that is, Initialization is required before use. The order of expression operators is similar to that of C language operators: Precedence OperatorsHighest ++, -- (used as prefixes); (), +,-(unary ),!, ~ *,/, % +,-<, >><, >,< =, >== ,! = & ^ | & | =, * =,/=, % =, + =,-=, <=, >>=, & =, ^ =, | = Lowest ++, -- (used as suffixes) the control flow allows the use of goto statements. The type returned by the conditional expression must be bool. For example, if (10) return false; // This statement cannot be compiled. When using switch-case, the usage of c ++ is different. For example: copy code switch (testVar) {case var1: // execute code... // if there is no break statement, the compiler cannot pass, but in c ++, // if you want it to continue to execute the following case, the "goto case var2;" statement must be added. // Of Course, if no statement is executed under case var1, It is also reasonable to use case var2: // execute code... break; default: break;} the copy code loop statement is similar to the C ++ variable Type conversion. Type Can safely be converted tobyte short, ushort, int, uint, long, ulong, float, do Uble, decimalsbyte short, int, long, float, double, decimalshort int, long, float, double, decimalushort int, uint, long, ulong, float, double, decimalint long, float, double, decimaluint long, ulong, float, double, decimallong float, double, decimalulong float, double, decimalfloat doublechar ushort, int, uint, long, ulong, float, double, in addition to the above implicit conversions, decimal also has display conversions. To prevent overflow, you can use the checked (expression) expression, for example, byte destVar; short srcVar = 281; destVar = checked (byte) srcVar ); or enable the default conversion detection mechanism directly in the project options. As shown in: some complex variable types, Enumeration, define an enum, as follows: copy the code enum orientation: byte // byte can be replaced by other integer types, such as int and long {north, if you copy code from south, east, west}, the method used to declare an enumeration type is orientation myDirect = orientation. north; directly output the result of myDirect: north. To output the specific value of the byte type, you must use the displayed type conversion: (byte) myDirect. You can also convert the "north" string to the enumeration type. The method is slightly complicated, as shown in the following code: string myStr = "north"; orientation myDirect = (orientation) Enum. parse (typeof (orientation), myStr); the difference between the structstruct type and C ++ is that the variable type is not public by default. But private. The Arrays array is declared as follows: <baseType> [] <name>;, for example, int [] myIntArray = {1, 2, 3 };, int [] myIntArray = new int [5];. It cannot be declared in the following way: <baseType> <name> []; The syntax structure of multi-dimensional arrays also has its own particularity. The declaration method is as follows: <baseType> [,] <name>; for example, double [,] hillHeight = new double [3, 4];. In a multi-dimensional array, the sorting order of each data is Row-first, for example, copy the code double [,] hillHeight = {1, 2, 4}, {2, 3, 4}, {3, 4, 5, 6 }}; foreach (double height in hillHeight) {Console. writeLine ("{0}", height);} // The output result is: // [0, 0] // [0, 1] //... when the data volume of each row is not the same, you can use Arrays of Arrays. When using an array, it cannot be used like a multi-dimensional array, for example, int [] [] jarged; jarged = new int [3] [4]; // during the compilation process, the error 'cannot implicitly convert type 'int' to 'int [] [] 'may be returned in two ways. For example, copy the sample code, such as maid = new int [2] []; maid [0] = new int [3]; and maid [1] = new int [4]; // or like belowjagged = {new int [] {, 3}, new int [] {1}, new int [] {, 6, 7 }}; when copying code, you also need to note that the following method cannot be used: copy the code foreach (int val in jarged) // a compilation error occurs, you cannot convert int [] to int {Console. writeLine (val);} // so it should be changed to the following method: foreach (int [] valArray in jarged) {foreach (int val in valArray) {Console. writeLine (val) ;}} copy the code to operate on String string String str = "hello world"; common: str. trim ();, str. trimStart (), str. trimEnd (), str. toLower (), str. padLeft (10, '-'), str. split ({''}) exercises output strings in reverse order, recursively copying the code public static void printReverse (string str, int I) {if (I <str. length) {printReverse (str, I + 1); Console. write (str. substring (I, 1);} return ;}

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.