Cainiao c # Learning tour ------ 01,

Source: Internet
Author: User

Cainiao c # Learning tour ------ 01,

I. Exchange the values of two numbers

1 // exchange the value of two numbers 2 # region method 3 int num1 = 10, num2 = 20; // The two values to be exchanged 4 int temp; // Temporary Variable 5 temp = num1; 6 num1 = num2; 7 num2 = temp; 8 Console. writeLine ("num1 is: {0}, num2 is: {1}", num1, num2); 9 Console. readKey (); 10 # endregion11 12 13 14 # region method 215 int num1 = 10, num2 = 20, temp; 16 temp = num1 + num2; 17 num1 = temp-num1; 18 num2 = temp-num2; 19 Console. writeLine ("num1 is: {0}, num2 is: {1}", num1, num2); 20 Console. readKey (); 21 # endregion

II. c # access modifier

C # There are five access modifiers: public, private, protected internal, and protected internal.

Access Modifier Description
Public Public access. Not limited.
Private Private access. Only access by members of this category is allowed.
Protected Protect access. The instance cannot be accessed only by the class or subclass.
Internal Internal access. It is restricted to access within the project, and others cannot be accessed.
Protected internal Access is protected internally. Only access to this project or subclass is allowed.

Iii. Methods

1. Method Name
* Specification: the first letter of each word is capitalized.

2. Return Value of the Method

* Void: no return value. You cannot use return to return specific values, but you can use return to terminate the current method.
* Non-void: the value of the specified type must be returned. There are two exceptions: double -- float/int parent class type -- Return subclass.

3. Call Methods

* Method call:

Static: The address is allocated only once. Static members are generated together with the class, so when there are static members, non-static members have not yet been generated, so they cannot be called

* If a static keyword is added to a method, it is a static method. Otherwise, it is a non-static method.

* Call methods in the same class:

1. static data can be called directly.

2. static data cannot be called directly.

3. Non-static users can directly call static

4. If you need to call a non-static member statically, you must use the instance Member.

5. Non-static data can be called directly.

* Method overloading:
* Premise: the method name must be the same
* Condition: the parameters are different (two parameters are different: the numbers or types are different)
* It has no relationship with the access type and Return Value Type of the method.

* Variable parameter: the number of parameter values can be any 0 ~ Multiple
1. When a variable parameter is called, it is not necessary to encapsulate the passed value into an array. The variable parameter will automatically create an array of proper length for you.
2. If no parameters are passed, an array with a length of 0 will be generated-the index is out of bounds.
3. The real parameter values of variable parameters must be of the same type.
4. variable parameters must be the last in the parameter list

 

 

 

To be continued...

 

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.