CSHARP + Asp.net series tutorial (5)

Source: Internet
Author: User
For more information, see C # and ASP. NET. Program Design tutorial writing, please point out any shortcomings, or leave a message on the old cat's ideal blog.

The long holiday is approaching. The wallet is also empty, and you have to invest in daunting work... although there are a lot of emotions, but the tutorial still needs to continue to write, a few complaints first. ^ _ ^, But the tutorial may be slower to write in the future, because I have more than enough power! Without talking nonsense, go to the topic:

Some netizens said that the tutorial was too cumbersome. The following is an analysis of the process control statement. If there is a C program design foundation, I will skip this section. I would like to give a brief introduction to some new users, for details, please refer to Professor Tan haoqiang's book "C language programming". It is very strong and worth reading.

In C #, there are two options: If statement and switch statement.

1. If (Boolean expression)

{

Embedded statement;

}

Else if (Boolean expression)

{

Embedded statement;

}

Else

{

Embedded statement;

} // When the value of the Boolean expression is true, execute the embedded statement after if.

Let's take a small example. Otherwise, I always think it is not thorough enough. Note that the main () method with parameters and the isdigit method of char are introduced in the example.

Using system;

Class mikecat

{

Public static void main (string [] ARGs)

{

If (ARGs. length! = 1) // Boolean operation for determining the number of parameters

{

Console. writeline ("the command line parameter can only be one ");

}

Else

{

Char c = ARGs [0] [0]; // The first dimension is the index of the first parameter, and the second dimension is the index of the Character Count of a parameter. I wonder if it is correct. I have not found any relevant information. What do you think ??

If (C> = 'A') & (c <= 'Z '))

{

Console. writeline ("{0} is an uppercase letter", C );

}

If (C> = 'A') & (c <= 'Z '))

{

Console. writeline ("{0} is a lowercase letter", C );

}

If (char. isdigit (c ))

{

Console. writeline ("{0} is a number", C );

}

}

}

}

The main method is the entry point of the program. The program control starts and ends in the method. This method is declared inside the class or structure. It must be static. It can have the void or Int return type. Create an object in the main method and call other methods. When declaring the main method, you can neither use the parameter nor use the parameter.

The main method can be of the void type:

Static void main ()

{

}

It can also return INT:

Static int main ()

{

Return 0;

}

Parameters can be used in the main method. In this case, they take one of the following forms:

Static int main (string [] ARGs)

Static void main (string [] ARGs)

The main method parameter represents the string array of the command line parameter. You can test the Length attribute to check whether a parameter exists. For example:

If (ARGs. Length = 0)

{

Console. writeline ("Please enter a numeric argument .");

Return 1;

}

You can also use the convert class or parse method to convert string parameters to numerical values. For example, the following statement converts a string to a long number using the parse method in the int64 class:

Long num = int64.parse (ARGs [0]);

You can also use the C # type long with the alias int64:

Long num = long. parse (ARGs [0]);

You can also use the convert method toint64 to do the same job:

Long num = convert. toint64 (s );



Char. isdigit Method

Indicates whether a Unicode Character belongs to the decimal number category.

Public static bool isdigit (char );

Indicates whether the character at the specified position in the specified string belongs to the decimal number category.

Public static bool isdigit (string, INT );

Using system;

Public class isdigitsample {

Public static void main (){

Char CH = '8 ';

Console. writeline (char. isdigit (CH); // output: "true"

Console. writeline (char. isdigit ("sample string", 7); // output: "false"

}

}

2. Switch (control expression)

{

Case constant expression:

Embedded statement;

[Break;]

[Goto case constant expression]

...

Default:

Embedded statement;

} // The switch statement is a variant of the IF statement. If you compare a variable or expression with many different values and execute different program segments based on different comparison results.

Note that if you want to implement direct functions like in C/C ++, use the Goto case and goto default jump statements.



The statement is used to repeatedly execute one or more rows. Code . C # contains four loop statements: while, do... while, for, and foreach.

1. While (Boolean expression)

{

Embedded statement;

} // Calculate the value of a Boolean expression. If the Boolean expression is true, execute the nested statement once.

If you do not want to give an example, you need to pay attention to the following: The while statement is executed repeatedly for zero or multiple times with conditions. In the while statement, you can use the break statement to immediately end the loop. You can also use the continue statement to stop the execution of embedded statements and continue the next loop.

2. Do... while statement

The difference is that the while statement must first execute an embedded statement before checking the Boolean expression.

3. For statement

For (initializer; condition; iterator)

{

Embedded statement;

} // Initializer, condition, and iterator are optional. Initializer is used to initialize the cyclic control variable. One or more variables can be separated by commas (,). condition is a cyclic control condition, and one or more statements can be used; the iterator regularly changes the value of the cyclic control variable.

4. foreach statement

The foreach statement is introduced from C # and does not exist in C/C ++. Foreach is used to enumerate each element in the set and execute embedded statements for each element.

Foreach (type identifier in expression)

{

Embedded statement;

} // Type and identifier are used to declare cyclic variables. The expression corresponds to the set to be enumerated.

Using system;

Using system. collections;

Class mikecat

{

Public static void main ()

{

Idictionary envvars = environment. getenvironmentvariables ();

Console. writeline ("A total of {0} environment variables", envvars. Keys. Count );

// Cyclically output each environment variable and Its Value

Foreach (string K in envvars. Keys)

{

Console. writeline ("{0} = {1}", K, envvars [K]. tostring (); // or envvars. Value

}

}

}

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.