01-C # Getting Started (function 1)

Source: Internet
Author: User

Only when writing code can you really understand the logic of the Code.So, start writing code.

Function meaning:Reduce code duplication for the same function and improve the maintenance efficiency of repetitive code.

  • Function

A file consists of the structure of the Command Space (), Class (), and function. The function is placed in the Class, and the static void HelloFunction () {} statement is used. The function name generally starts with an uppercase letter. The function name and brackets must be used to call the function: HelloFunction ();.

Void indicates that the function has no return value. to return a value, replace void with the returned value type. Use return to end the function and return the value to the calling code. In addition, return can be stored in the void declared function, for example: return ;().

Parameter designation: int I, string myStr. You can also use params to specify the parameter array, which must be placed at the end of the parameter list. Pass the parameter value transfer and reference transfer, and pass the reference (). The function modifies the value of the input variable, for example:

Static void DoubleInt (ref int refInt) {refInt * = 2; Console. writeLine ("ref function value: {0}", refInt);} static void Main (string [] args) {// ref reference passed, because the parameter value will be modified, the referenced parameter has two conditions: 1) cannot be a constant; 2) must be initialized; int refVal = 10; Console. writeLine ("value of the variable before ref: {0}", refVal); DoubleInt (ref refVal); Console. writeLine ("ref parameter value result: {0}", refVal );}
  • Variable Scope

Variables declared in different functions cannot be directly accessed in other functions. You need to use parameters. In addition, variables can be declared in the class. The same way is static. For example, if the variable name is the same as that in the function, you must use Program (class name ). myStr access. If myStr is used directly, global variables are blocked.

Note that statement blocks also affect the scope of variables. for example, for (int I = 1), this I can only be used in the for loop (). After jumping out of the loop, variable I cannot be used.

I have reserved my opinion on the use of global variables. We recommend that you use the parameter transfer method between functions, so that you can intuitively see the changes in variables.

  • Main () entry function

Executing the C # program is equivalent to executing the main () function. After the main () function is executed, the program execution process ends. The main function can return the int () value, indicating how the program exits and how to use it later. In addition, the most interesting parameter is string [] args, Which is intuitive for DOS Programs:

Static void Main (string [] args) {Console. writeLine ("parameter length: {0}", args. length); foreach (string myArg in args) {Console. writeLine ("*) {0}", myArg );}}

You can configure the project properties: Debug-start option-command line parameters to assign values to args. parameters are separated by spaces. If a string contains spaces, It is enclosed by quotation marks. Try to use it in DOS, and add parameters like the doscommand.

  • Structure functions

You can declare a function in the structure and do not use static instead of public when declaring the function. It should be a need for accessibility ():

    struct customerName    {        public string firstName, lastName;        public string cusName()        {            return firstName + lastName;        }    }

This can be used in this way: customerName. firstName = "sailor", customerName. cusName (), a little object-oriented.

  • Conclusion

In the past, when I learned that the command line program was "originally made in this way", I was not in the mood to learn it, and then I went to the command line program as self-righteous, as a result, I lost my interest in learning. The result was abandoned by a half-distance.

SO, now it's time to calm down ~~~Making truly usable tools is a process and a process of continuous iteration.

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.