C # basic finishing 3 Final Version

Source: Internet
Author: User

1. Enumeration and constants

(1) constant Syntax: const type variable name = variable value; for example, const int pi = 3.14;

Assign values when defining. values cannot be assigned elsewhere.

(2) Enumeration

Let's define an enumeration type and specify all values of this type when defining this type.

Syntax: enum type name {value 1, value 1, value 3 ..... Value n}

Note: The enumeration definition is generally at the same level as the class definition, this enumeration can be used for all classes in the same namespace (in methods/classes ).

Example: enum Gender {male, female}

In the main function, Gender sex = Gender. Male is called;

Switch (sex)

{

Case Gender. Male:

Console. WriteLine ("male ");

Break;

Case Gender. FEMALE:

Console. WriteLine ("female ");

Break;
}

Enumeration function: 1) restrict users from assigning values at will. You can only select the enumerated values when defining enumeration.

2) You do not need to memorize what each value is. You only need to select the corresponding value.

Note: 1) when defining enumeration, the value cannot be of the int type.

2) variables of the enumeration type can be forcibly converted to an int type.

3) The enumerated value is defined with a default number starting from 0.

How to convert a string to an enumeration type:

(Self-defined Enumeration type) (Enum. Parse (typeof (self-defined), "string to be converted "));

  1. Struct

(1) Why use a structure?

1) For example, to store a person's information, we need to declare a set of variables. When we want to store the information of several people, we need to declare n sets of variables, which is very troublesome.

2) the variables that store a person's information have no relationship with each other and are easy to remember.

Syntax: access modifier struct structure name

{

Define structure members;

}

After defining a structure, you can directly declare the corresponding variables. After declaring the variables, you can use the variable name. member name

  1. Array

1) arrays help us declare multiple variables of the same type at a time. These variables are continuously stored in the memory.

2) array declaration Syntax: Data Type [] array name = new data type [array length];

For example, int [] score = new int [5];

Annotation: declares an array named score. In layman's terms, an array is declared, which contains five int-type variables. The array name is score. The five int-type variables in the array are called array elements.

3) how to access the array: access the array by subscript (index. Array name [subscript];

4) once an array of the int type is declared, each element in the array is initialized to 0.

5) The Length of the array can be obtained through the array name. Length.

6) Console. clear (); clear the information on the screen

  1. Bubble Sorting

Let the elements in the array be compared by two (I and I + 1), after n (I-1), the elements in the array can be sorted according to our expected law, to sort data in ascending order, we use "<" for comparison.

For example:

Int [] scores = {23, 45, 67, 67, 87, 9, 8, 89 };

For (int I = 0; I <scores. Length-1; I ++)

{

For (int j = 0; j <scores. Length-1-I; j ++)

{

If (scores [j] <scores [j + 1])

{

Int temp = scores [j];

Scores [j] = scores [j + 1];

Scores [j + 1] = temp;

}

}

}

For (int I = 0; I <scores. Length; I ++)

{

Console. WriteLine (scores [I]);

}

Console. ReadKey ();

  1. Method (function) Introduction

(1) A method is a mechanism for reusing a bunch of code. A method is a piece of code. This code may have input values (parameters) and may return values, A method is like a person who specializes in this task. We call it to do something. It may require us to provide some data to it, after the execution is completed, some execution results may be returned to us. The required data is called a parameter, and the returned execution result is called a return value.

(2) Console. ReadLine (); is a function with returned results.

Console. WriteLine ("Hello"); is a function with execution parameters. It knows how to execute only the data that tells WriteLine () to be executed,

Int I = Convert. ToInt32 ("22"); is a function with both parameters and return values.

(3) With methods (functions), writing code is like building blocks. Various technologies in C # actually use, these basic syntaxes, such as if, organize different methods according to certain logic.

(4) function: Used to reuse code. When we repeatedly write the same code in a program, we can define the code that needs to be repeatedly written in a method. It is okay to call the code when it is used.

Syntax of method definition: [access modifier] [static] Return Value Type method name ([parameter])

{

Method body;

}

Note: 1) methods are generally defined in the class.

2) If the method does not return a value, the return value type is void.

3) if the method does not have parameters, it cannot be omitted ().

(5) Naming rules: the method name must start with an upper-case name, and the parameter name must start with a lower-case name. The parameter name and variable name must be meaningful.

(6) Call A method. For a static method, if it is in the same class, simply write the name to call it.

(7) return can exit the method immediately.

  1. Scope of the variable (lifecycle of the variable)

(1) A variable defined in a method is called a local variable. Its scope begins with definition and ends with braces.

(2) What should I do if I want to access the variables in another method in one method?

Two solutions are available: parameters and return values.

(3) define a variable in the brackets behind the method name, which is called a parameter for defining this method. The variables defined here are used to receive information from callers.

Note: If a parameter exists in a method, the caller must pass the parameter, and the number of parameters must be consistent with the type at the corresponding position.

  1. Return Value

(1) When the caller wants to access the variables in our method, the return value can be returned. For example:

String s = Console. ReadLine (); int I = Convert. ToInt32 ("22 ");

(2) Why can a variable be defined before a method receive the value of a method because the return value is used in the method, or as long as the return value exists in the method, then in the call method, we should use a variable to receive the return value of the method.

Note: One method can only return one value.

(3) Once a method has a return value, a value must be returned through the return statement in this method value, and this value must be of the same type as the return value.

Syntax: return value;

Note: Once a variable is defined outside the method, it is inside the class. It is called a class field. This variable can be accessed by all methods of this class, but note: static methods can only access static fields.

  1. Method Overloading

(1) What is method overload?

In the same class, a method overload can be formed only when the method name is the same and the number of method parameters is different or the type at the corresponding position is different.

Note: There is no relationship between method overload and return value.

  1. Out parameters and ref PARAMETERS OF THE METHOD

(1) return values through parameters.

Static voidMain (string [] args)

{

Int number;

Int result = Test (out number );

Console. WriteLine ("number = {0} result = {1}", number, result );

Console. ReadKey ();
}

Public static int Test (out int)

{

A = 20;

Return;

}

The implementation steps are as follows:

1) if you add out before the parameter type of the method, you must add out before number when passing the parameter. It indicates that this parameter is not passed in, but used for outgoing value.

2) If the parameter is passed in as an out parameter, the initial value can be left blank before the parameter is passed in.

3) parameters modified by out in the method must be assigned a value before use.

Example: string s = "123"; int re;

If (int. TryParse (s, out re) = true)

{

Console. WriteLine ("successfully converted" + re );

}

Else

{

Console. WriteLine ("Conversion failed !");

}

For example, the implementation of myTryParse writes a MyTryParse method, requiring the user to enter a string. If the string can be converted to the int type, the method returns true, in addition, the converted int type data is transmitted through the method parameters. If the string cannot be converted to the int type, the method returns false, and the output parameter is meaningless, can I assign three values to a method at will?

Static voidMain (string [] args)

{

String s = "123sds ";

Int re;

If (IntTryParse (s, out re ))

{

Console. WriteLine ("Success |! Why? "+ Re );

}

Else

{

Console. WriteLine ("loss ° failed" failed! Why? ");

}

Console. ReadKey ();

}

Static bool IntTryParse (string s, out int result)

{

Result = 0;

Try

{

Result = Convert. ToInt32 (s );

Return true;

}

Catch

{

Return false;

}

}

(2) ref Parameters

1) out is used for outgoing values. In the method, the out parameter must be assigned an initial value.

2) ref can be understood as bidirectional and can be passed in or out.

3) when the parameter is re-transmitted, if the parameter has an out or ref modifier, the value of the parameter variable in the method will be changed, and the value in the caller's method will also change accordingly.

Example: return values of array parameters.

Static voidMain (string [] args)

{

Int [] nums = {1, 3, 2, 5, 4, 6 };

Int max, min, sum;

Sum = compute (nums, out max, out min );

Console. WriteLine ("the sum of Arrays: {0} the maximum value is {1} and the minimum value is {2}", sum, max, min );

Console. ReadKey ();

}

Static int compute (int [] numbers, out int max, out int min)

{

Int sum = 0;

Max = numbers [0];

Min = numbers [0];

For (int I = 0; I <numbers. Length; I ++)

{

Sum + = numbers [I];

If (numbers [I]> max)

{

Max = numbers [I];

}

If (numbers [I] <min)

{

Min = numbers [I];

}

}

Return sum;

}

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.