Basic concepts ---- Beginning Visual C #,
For more information, see my personal homepage: zhongxiewei.com variable.
Annotation method:// Comment here
And/* Comment here */
Integer variable type:
Type |
Alias |
Allowed Values |
Sbyte |
System. SByte |
Integer between-2 ^ 7 and 2 ^ 7-1 |
Byte |
System. Byte |
Integer between 0 and 2 ^ 8-1 |
Short |
System. Int16 |
Integer between-2 ^ 15 and 2 ^ 15-1 |
Ushort |
System. UInt16 |
Integer between 0 and 2 ^ 16-1 |
Int |
System. Int32 |
Integer between-2 ^ 31 and 2 ^ 31-1 |
Uint |
System. UInt32 |
Integer between 0 and 2 ^ 32-1 |
Long |
System. Int64 |
Integer between-2 ^ 63 and 2 ^ 63-1 |
Ulong |
System. UInt64 |
Integer between 0 and 2 ^ 64-1 |
Floating point type:
Type |
Alias |
Approx Min Value |
Approx Max Value |
Float |
System. Single |
1.5x10-45 |
3.4x1038 |
Double |
System. Double |
5.0x10-324 |
1.7x10308 |
Decimal |
System. Decimal |
1.0x10-28 |
7.9x1028 |
Other simple types:
Type |
Alias |
Allowed Values |
Char |
System. Char |
Single Unicode char, between 0, and 65535 |
Bool |
System. Boolean |
True or false |
String |
System. String |
A sequence of characters |
Variable naming:
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, a literal constant:
"C:\\Temp\\mydir\\myfile.doc"
Equivalent@"C:\Temp\mydir\myfile.doc"
In addition, you can enter a string across rows, such:
@"first linesecond linethird line"
The use of variables has a requirement in many languages, that isInitialization is required before use..
Expression
The operator is similar to the C Language
Operator order:
Precedence |
Operators |
Highest |
++, -- (Used as prefixes); (), +,-(unary ),!, ~ |
|
*,/, % |
|
+ ,- |
|
<,> |
|
<,>, <=,> = |
|
= ,! = |
|
& |
|
^ |
|
| |
|
&& |
|
| |
|
=, * =,/=, % =, + =,-=, <=, >>=, & =, ^ =, | = |
Lowest |
++, -- (Used as suffixes) |
Control Flow
The goto statement is allowed. The type returned by the conditional expression must bebool
. For example:If (10) return false; // This sentence cannot be compiled
When using switch-case, the usage of c ++ is different, for example:
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 ;}
Loop statements are similar to C ++ statements.
More variable-related type conversion
Type |
Can safely be converted |
Byte |
Short, ushort, int, uint, long, ulong, float, double, decimal |
Sbyte |
Short, int, long, float, double, decimal |
Short |
Int, long, float, double, decimal |
Ushort |
Int, uint, long, ulong, float, double, decimal |
Int |
Long, float, double, decimal |
Uint |
Long, ulong, float, double, decimal |
Long |
Float, double, decimal |
Ulong |
Float, double, decimal |
Float |
Double |
Char |
Ushort, int, uint, long, ulong, float, double, decimal |
In addition to the above implicit conversions, there are also display conversions. To prevent overflow, you can usechecked(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:
Enum orientation: byte // byte can be replaced by another Integer type, such as int and long {north, south, east, west}
The following method is used to declare an enumeration type:orientation myDirect = orientation.north;
; The direct output result of myDirect is: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 below:
string myStr = "north";orientation myDirect = (orientation)Enum.Parse(typeof(orientation), myStr);
Struct
The difference between the struct type and C ++ is that the variable type is not public by default. But private.
Arrays
The array declaration method is as follows:<baseType>[] <name>;
Such: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 statement is as follows:<baseType>[,] <name>;
Such:double[,] hillHeight = new double[3,4];
. In a multi-dimensional array, the data is sorted first by row, for example:
Double [,] hillHeight = {1, 2, 3, 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] //...
Arrays of Arrays can be used when the data volume of each row is not equal. When using an array, it cannot be used like a multi-dimensional array, for example:
Int [] [] jagged; jagged = new int [3] [4]; // during compilation, the error 'could not implicitly convert type 'int' to 'int [] [] 'may occur.
There are two methods to implement the Declaration. For example:
jagged = new int[2][];jagged[0] = new int[3];jagged[1] = new int[4];// or like belowjagged = {new int[] {1,2,3}, new int[] {1}, new int[] {4,5,6,7}};
When traversing it, you also need to note that the following method cannot be used:
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 );}}
String operations
string str=" hello world ";
Common examples include:str.Trim();
,str.TrimStart()
,str.TrimEnd()
,str.ToLower()
,str.PadLeft(10, '-')
,str.Split({' '})
Exercise
public static void printReverse(string str, int i){ if (i < str.Length) { printReverse(str, i + 1); Console.Write(str.Substring(i, 1)); } return;}
Why does visual c prompt that C is not defined?
You should note that function calls are passed values, so even if you modify the values of a, B, and c in the input, the values of a, B, and c in the main program will not change. We recommend that you modify the parameters as follows:
1: CHANGE a, B, and c to global variables.
2: change input to void input (float * a, float * B, float * c). input (& a, & B, & c) is used for calling );
Beginning visual c 2012 PROGRAMMING
It seems that textbooks written by foreigners are better understood than those written by Chinese people, and the book will show the words "if you have not fully mastered this chapter, don't be alarmed", which is really friendly. The examples in each section are arranged very well ......