C # knowledge point summary of a kindergarten class,

Source: Internet
Author: User

C # knowledge point summary of a kindergarten class,
C # knowledge point summary

(In fact, C # is different from Java, for a large kindergarten student)

1. Understand C # Program

(1) namespqce keyword

Namespqce (namespace) is a C # code organization method, which is similar to the package (package) in Java ),

In this way, we can put closely related code in the same namespace, greatly improving the management and use efficiency.

(2) using keywords

In Java, if other packages are imported, the import keyword should be used. In C #, use the using keyword to reference other namespaces,

It is similar to the import in Java.

(3) clas keywords

Like Java, C # is also an object-oriented language that uses the class keyword to represent classes. The code we write should all be included in a class,

Class must also be included in a namespace. When a Program template is generated, Visual Studio automatically creates a class named Program. If you

If you don't like it, you can get rid of it. Note: C # is different from Java, and the class name must be the same as the source file name.

(4) Main () method

The Main () method in C # serves the same purpose as the main () method in Java. It is the entry for program running and the application starts to run from here.

Note that the first letter of the Main () method in C # Must be capitalized, And the return value of the Main () method can be of the void or int type,

The Main () method can have no command line parameters. Therefore, the Main () method in C # has four forms.

Syntax:

Statle void Main (string [] args ){}

Statle int Main (string [] args ){}

Statle void Main (){}

Statle int Main (){}

When creating a project, the compiler creates a Main () method in the first form.

(5) key code

The two lines of code added in the Main () method are the key code of this applet, which is used for output and input.

 

1 Console. WriteLine ("Hello World"); // output content from the Console 2 Console. ReadLine (); // enter content from the Console

 

2. Variables and constants in C #

C # Common Data Types

Common Data Types Java C # Example
Integer Int Int Age
Floating Point Type Fioat Float Score
Double Precision type Double Double Circumference Rate
String type String String Name
Boolean Boolean Bool Whether it is a minority

Note: The Boolean keywords in C # are different from those in Java. Use the Boolean keywords in bool and C # To be lowercase.

When assigning values to floating-point variables, the C # language requires that the assignment data end with the letter f or F.

C # VARIABLES

The variable declaration method in C # is the same as that in Java. Use the method described below.

Syntax:

Data type variable name;

The naming rules for variables in C # are basically the same as those in Java, but the "$" symbol cannot be used in C #. Therefore, the naming rules for variables can be summarized as follows.
① Composition: 52 English letters (~ Z, ~ Z), 10 digits (0 ~ 9), underscore (_), cannot contain other characters.

② Start: it must start with a letter or underline.

③ Unavailable: it cannot be a keyword in C.

Constants in C #

Let's look at an example.

1 namespace HelloWorld 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 int dayMax = 7; // The number of days per week 8 int today = 1; // today's day of week 9 Console. writeLine ("several days a week:"); 10 Console. writeLine (dayMax); // number of output days 11 Console. writeLine ("Today is the week:"); 12 dayMax = dayMax + 1; 13 Console. writeLine (dayMax); 14} 15} 16}

 


Syntax:
Const data type constant name = value;
For example:
Public const int dayMax = 7; // defines the constant dayMax.
3. Classes and objects
Syntax:
[Access modifier] Return Value Type method name (parameter list)
{
// Method subject
}
(1) access modifier
Public (public) private (private ).
(2) Return Value Type of the Method
The method we compile is to implement a specific function module that can be called by others. After the call, a value can be returned,
The data type of this return value is the return value type of the method, which can be int, float, double, bool, string, etc.
If the method does not return any value, use the void keyword.
Understanding classes and objects
Syntax:
[Access modifier] class name
{
// Subject of the class
}

Select Structure

If Structure

Syntax:

If (conditional expression)

{

// Code block

}

Syntax:

If (conditional expression)

{

// Code block 1

}

Else

{

// Code block 2

}

Switch Structure

C # The language requires that each case and default statement must have a break statement, unless there are no other statements in the two cases,

The previous case may not contain break. In addition, the switch judgment in C # is more flexible, and the judgment expression or variable is

It can be a string type.

(1) In the switch structure, case clauses are placed in no order. You can put the default clause at the beginning,

However, note that no two case statements can have the same value.

(2) the value in the case clause must be a constant expression and variables cannot be used.

Array and loop

Array

Syntax:

Data Type [] array name;

Example:

Int [] array;

 

Print triangle

1 using System; 2 public class Hello 3 {4 public static void Main () 5 {6 Console. write ("Enter the number of rows:"); 7 int lines = int. parse (Console. readLine (); 8 Console. writeLine (""); 9 for (int I = 1; I <= lines; I ++) 10 {11 for (int k = 1; k <= lines-I; k ++) 12 Console. write (""); 13 for (int j = 1; j <= I * 2 + 1; j ++) 14 Console. write ("*"); 15 Console. writeLine (""); 16} 17 Console. readLine (); 18} 19}


Bubble Sorting

1 using System; 2 class ArraySort 3 {4 public static void Main () 5 {6 int [] d = {,}; 7 int temp; 8 // sort 9 for (int I = 0; I <d. length; I ++) 10 for (int j = I + 1; j <d. length; j ++) 11 if (d [I] <d [j]) 12 {13 temp = d [I]; 14 d [I] = d [j]; 15 d [j] = temp; 16} 17 // output sorting result 18 foreach (int I in d) 19 Console. write ("{0},", I); 20 Console. read (); 21} 22}

 

Time is too short.

Please wait for the next Summary

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.