s1/c# Language and database technology Fundamentals/01-First C # program

Source: Internet
Author: User
Tags constant definition readline

When you execute a C # application, you can use the following shortcut keys provided by the. NET Framework.

F5: Start.

Ctrl+f5: Start execution without debugging.

Ctrl+atl+l: Display Solution Explorer, or click the menu bar to select the tools → options options dialog box, select the Projects and Solutions option on the left, and tick the Always Show solutions check box on the right.

Understanding C # Programs

    1. The namespace keyword, namespace (namespace) is the way in which code is organized in C #, which acts like a package in Java, so that we can put some tightly related code in the same namespace, greatly improving the efficiency of management and use.
    2. Using keyword, in Java, if you import other packages, you should use the Import keyword. In C #, the USE keyword is used to refer to other namespaces, and it works like import in Java.
    3. Class keyword, like Java, C # is an object-oriented language that uses the class keyword to represent a class. We write code that should be contained within a class that is contained within a namespace. When the program template is generated, Visual studio automatically creates a class called programs. If you don't like it, you can get rid of it. Note: Unlike Java, C # does not require the class name to be the same as the name of the source file.
    4. The main () method in C # works the same as the main () method in Java, which is the portal where the application runs. Note, however, that the first letter of the main () method in C # must be capitalized, the return value of the main () method can be void or int, and the main () method can have no command-line arguments. So the main () method in C # has four types:

static void Main (string[] args) {}

static int Main (string[] args) {}

static void Main () {}

static int Main () {}

When creating a project, the compiler creates a first form of the main () method.

Note: When writing a program, it is common to use the main () method, which is automatically generated by the compiler, and can be modified to three other forms as needed.

For example, if you want to pass some parameters through the command line when the main () method is called, you can use the main () method with parameters;

The main () method with a return value can be used when the main () method execution ends with a return result (such as success or failure).

5, the key code, the Main () method adds two lines of code is the key code for this applet, is used to output and input.

Console.WriteLine ("Hello World"); Output content from the console

Console.ReadLine (); Input from console

Contrast

C#

Java

How to Organize your code

Namespaces (namespace)

Packages (Package)

Introduce a keyword for a namespace or package

Using

Import

Program entry

Four forms of the main () method

A form of the main () method

Common data types for C #

Common types of data

Java

C#

Example

Integral type

Int

Int

Age

Floating point Type

Float

Float

Results

Double-precision Type

Double

Double

Pi

String type

String

String

Name

Boolean type

Boolean

Bool

Whether it is a minority

Note: The Boolean keyword in C # is different from Java, and the string type keyword in bool,c# is lowercase. When assigning a value to a floating-point variable, the C # language requires that the assignment data be terminated with the letter F or F.

How variables are declared in C #

Syntax: data type variable name;

C # 's variable naming conventions are basically the same as Java, except that the "$" notation is not available in C #, so the naming conventions for variables can be summarized in the following three simple terms.

1, Composition: 52 English Letters (A~Z,A~Z), 10 digits (0~9), underscore (_), in addition to other characters can not be included.

2. Start with: You can only start with a letter or an underscore.

3. Cannot be used: cannot be a keyword in C #, a complete list of keywords for C # see Appendix 2.

The naming conventions for variables are as follows.

1, the name of the variable to have meaning, as far as possible with the corresponding English name, with the meaning of the role of the name, for example, a variable representative name, do not use AA, BB, etc., to use the name.

2, avoid using a single character as a variable name, such as a, B, C, etc., should use INDEX,TEMP, etc., except for the loop variable.

3. When using multiple words to form variable names, the camel (camel) nomenclature should be used, that is, the first letter is lowercase, and the first letter of the other word is capitalized, such as Myname,yourage.

Constant: Is the value that remains unchanged while the program is running. The syntax for a constant definition is as follows.

Syntax: const data type constant name = value;

For example: public const int daymax=7; Defining Constants Daymax

Constants are the amount of changes that do not occur during a program's operation. If you force its value to be changed in the program, an error occurs. If you want to modify the value of a constant in your program, you can only modify it when you define it.

The constant naming conventions are as follows.

1, the constant name must have certain practical significance.

2, the name of the constant is best named in uppercase letters, the middle can be connected according to the meaning of the connection underlined, each constant definition of the right side of the best has a simple comment, explaining the effect.

3, the length of the constant name should not exceed 25 characters, otherwise the readability is poor.

Use of constants

1, used in the program once set the value is not allowed to be modified.

2. Values that are frequently referenced in the program.

3, used in the program has a specific meaning of the value.

There are three ways to output using the Console.WriteLine () method.

Mode one: Console.WriteLine ();

Mode two: Console.WriteLine (the value to be output);

Way Three: Console.WriteLine ("Format string", variable list);

C # read in from console

corresponding to Console.WriteLine (), The Console.ReadLine () method can be used from the console input. Write is the meaning of writing, and read is the meaning of degrees.

Grammar:

Console.ReadLine ();

The sentence returns a string that can be assigned directly to a string variable, for example:

Name=console.readline ();

What if you need to enter integer data? You just need a plastic transform.

int Age=int. Parse (Console.ReadLine ());

Int. The Parse () method is the function of converting a string to an integer.

The specific contents of the annotation specification are as follows:

1, the class name should be used before the document comments, explain the simple function of the class and how to use.

2, the method should use comments, explain the function of the method, the meaning of the parameters, return value and so on.

3. Critical statements use annotations, such as variable declarations, conditional judgments, loops, and so on.

Debugging Tips

1. Set breakpoints

Press the F9 key

The common shortcut keys for program debugging are as follows:

F5: Start Debugging

SHIFT+F5: Stop Debugging

F9: setting or canceling breakpoints

CTRL+F9: Cancel Breakpoint

F10: Single Step execution

F2: Goes to the definition of the called procedure or variable.

CTRL+F2: Shifts the focus to the drop-down list box for the class.

2. Monitor variable values

For example, use the Watch window to observe changes in the member variables of the object student.

Method One: Select and right-click the variable you want to follow, such as the object student member, and select the Add Watch option on the shortcut menu.

Method Two: In the Watch 1 window, click the blank cell under the name, enter "Student", and press ENTER.

s1/c# Language and database technology Fundamentals/01-First C # program

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.