Like C++,java, C # supports commonly used If,while,do while,for,switch statements, which are not described in detail, but they have some usages in C # that differ from other languages:
(1) In C #, an expression in an IF clause must be equal to a Boolean value. You cannot directly test an integer (such as a value returned from a function, you must explicitly convert the returned integer to a Boolean value of Ture or FALSE, for example, the value
Compare with 0 or null:
if (dosomething ()!=0) { } else { //return zero }
(2) In C #, you can use a string as a variable for testing. This differs from the switch statement in other languages. Like what:
String teststr = Func2 (); Switch(TESTSTR) {case ' for you ': Console.WriteLine ("For You"); Break; Case ' for everyone ': Console.WriteLine ("For Everyone"), break; default: Console.WriteLine ("Error!" ); Break;}
In addition, each case in C # 's switch can be empty or break after each case, and you can use Goto to jump between each case, for example:
int tsetnum=3; switch (testnum) {case 2; Testnum=6; Goto case 3; Case 3: testnum=9; default: Break;}
However, it is generally not recommended to use a goto statement because it causes confusion and makes the program difficult to debug.
Also, because C # appears to be later than many other languages, there are many new syntax statements that are commonly used in the following ways:
(1) foreach statement
The syntax for the Foreach loop can be understood in the following code, where Arrayofints is assumed to be an integer array:
foreach (int temp in arrayofints) { Console.WriteLine (temp);}
Where the Foreach loop is an element in each iteration of an algebraic group. It puts the value of each element in the variable temp of the integer type for output traversal. When the variable type is indeterminate, you can use the type inference function
Yes. This usage will be more visible. At this point, the Foreach loop becomes:
foreach (Var temp2 in arrayofvalues) { Console.WriteLine (TEMP2);}
Note that the Foreach loop cannot change the value of the items in the collection (above temp), so the following code is not compiled:
foreach (Var temp2 in arrayofvalues) { temp++; Console.WriteLine (TEMP2);}
If you need to iterate over the items in the collection and change their values, you should use a for loop.
(2) enum type enum
An enumeration is a user-defined integer type. When declaring an enumeration, you specify an acceptable set of values that an instance of the enumeration can contain. In addition, you can assign a value to a name that is easy to remember. If the
Somewhere in the code, to try to assign a value that is not in the acceptable range to an instance of the enumeration, the compiler reports an error. In the long run, creating enumerations can save a lot of time and reduce a lot of hassle.
public enum kindsofseason{ Spring = 1; Summer = 2; Autumn = 3; Winter = 4;}
This example uses an integer value in the enumeration to represent the four seasons. These values can now be accessed as members of the enumeration. For example, Kindsofseason.spring returns the number 1. Using this enumeration is generally
is to transfer the appropriate value to the method.
In C #, the real power of enumerations is that they are instantiated in the background as a struct that derives from the base class System.Enum. This means that you can invoke methods on them and perform useful tasks. Note Because
For the. NET framework execution, the syntax for enumerating enumerations means that you can invoke methods on them and perform useful tasks.
Other methods on System.enun can return the number of values in the enumeration definition or the names of the listed values, and so on.
Some common new sentence syntax in C # is introduced to this, which can only be counted as a small part of C # statement, the other needs in the actual reading and writing process to learn to accumulate.
The next blog will introduce you to the objects and classes of C #.
C # Getting Started share (iv)--c# common statements