C # basic sorting (2 ),

Source: Internet
Author: User

C # basic sorting (2 ),

1. Variable type
Int, double, string, char, bool, decimal
Variable usage rules: declare, assign values, and use

2. Naming rules:
Camel: lowercase for the first word and lowercase for other words
Pascal: the first letter of each word is in upper case, and the other letters are in lower case.

5. Constants
Syntax for declaring constants:
Const variable type variable name = value;
Example: public const int Che_SHI = 10001;
Once a constant is assigned a value, it cannot be assigned a value again.

6. enumeration (standardizing our development)
Syntax:
[Public] enum enumeration name
{
Value 1,
Value 2,
Value 3,
...........
}

[] Indicates that it can be omitted.
Public: access modifier, Public. Modify the access permissions of a member.
Enum: keyword, which declares the enumeration keyword
Enumeration name: To comply with pascal naming rules
Declare the enumeration to the namespace. The class outside the namespace indicates that all classes in the namespace can use this enumeration.
Enumeration is a variable type, such as int and string.
Only enumeration declaration, value assignment, and usage are different from those of common variable types.
Enumeration is a self-written variable type.

We can convert an enumerated type variable to an int type or a string type.
The enumeration type is compatible with the int type by default. Therefore, you can use the syntax of forced type conversion to convert each other.
When you convert a value that is not in an enumeration, the number is displayed directly instead of an exception.

Enumeration can also be converted to the string type. If you convert the enumeration type to the string type, you can directly call ToString ().
To convert a string to an enumeration type, you need the following code:
(Enumeration type to be converted) Enum. Parse (typeof (Enumeration type to be converted), "string to be converted ");
Call the Parse () method in the Enum class to convert a string to the corresponding enumerated type.
If the string to be converted is a number, it will not throw an exception even if it is not in the enumeration.
If the converted string is text, if it is not in the enumeration, an exception is thrown.


7. Structure
To declare multiple variables (fields) of different types at a time)
Syntax:
[Public] struct structure name
{
Public string name;
Member // Field
}

Multiple values can be stored in fields in the program, and only one value can be saved in variables.
Field naming rules. Each field is preceded by an underscore to distinguish between variables and fields.

8. Array
An array is used to store multiple variables of the same type at a time.
Syntax:
Array type [] array name = new array type [array length];
Int [] nums = new int [5];
Once the length of the array is fixed, it cannot be changed.

Array has initial values
Int array Initial Value 0
Null Initial Value of string Array (no space occupied)
Boolean array Initial Value false

Four writing methods
Int [] nums = new int [5];
Int [] nums = {1, 2, 3, 4, 5}; (commonly used)
Int [] nums = new int [5] {1, 2, 3, 4, 5 };
Int [] nums = new int [] {1, 2, 3, 4, 5 ,};

The maximum value in the int range is int. MaxValue.
Minimum int. MinValue
You can use subscript or index to access elements in the array.

9. Bubble Sorting
Sort the elements in an array in the ascending or ascending order.
Int [] nums = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
Array. Sort (nums); // Sort the Array in ascending order
Array. Reverse (nums); // sort the Array in Reverse order
No return value. An array is required as a parameter.

10. Methods/functions
A function is a mechanism for reusing a bunch of code.
Function Syntax:
[Public] Method Name of the static return value type ([parameter list])
{
Method body:
}
Public static int AddSum (int a, int B)
{
Return a + B;
}

Return Value Type: if you do not need to write the return value, write void. Return if return is returned
Parameter list: the parameters that must be provided to this method. Some methods do not need to write parameters.

11. return usage
1) return the value to be returned in the method.
2) terminate this method immediately.
3) For a layer-by-layer door, return returns only the layer-on-layer door.

12. In the Main () function, call the Test () function.
The Main () function is called the caller, and the Test () function is called the caller.
If Test () wants to get the value of Main:
1) pass parameters.
2) use static fields to simulate global variables.

If Main () wants to get the value of Test:
1) Return Value

13. Whether it is a real parameter or a form parameter, the space is opened in the memory.

14. Methods must have a single function.
Do not prompt user input in the method.

15. out, ref, params
1). out parameter.
If you return multiple values of the same type in a method, you can consider returning an array.
However, if multiple different types of values are returned, the returned array will not work. At this time,
We can consider using the out parameter.
The out parameter can return multiple different types of values in a method.
The out parameter must be assigned a value inside the method.

2) ref Parameters
A variable can be brought into a method for change. After the change is completed, the changed value is taken out of the method.
The ref parameter must be assigned a value outside the method, but not in the method.
Ref does not need to create a new variable to receive the return value.

Ref indicates inbound and outbound While outbound indicates outbound.
3) variable params Parameters
The elements in the real parameter list that are consistent with the variable parameter array are processed as elements of the array.
The params variable parameter must be the last element in the parameter list.

Public static void Test (string name, params int [] score)

16. Method Overloading
Concept: Method overloading means that the method name is the same, but the parameters are different.
There are two different parameters:
1) if the number of parameters is the same, the parameter types cannot be the same.
2) If the parameter type is the same, the number of parameters cannot be the same.
* ** The method overload has nothing to do with the return value.

Which parameter is used?
The overload of the method means that any parameter can be placed.

17. String usage
The length of a string can be used to compare the length of a word.

18. Change the array in the method without the return value. Arrays are an exception in methods.

19. Several Cosole Methods
1) Change the font color
Console. ForegroundColor = ConsoleColor. Yellow;
2) console. clear (); clear screen
3) console. readkey (true); the output button is not displayed.
4) Retain the last two digits of the decimal point in the output.
Double avg = 10/3;
Console. WriteLine ("{0. 00}", avg );

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.