Define C # method calls and commissioning

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43425487/article/details/102752756

This Section

1. The origin of the method;

2. Definition and calling methods;

3. builders (a special method);

The method of overload (Override);

5. How to Debug method;

6. The method of calling and stack *

* Recommended books: CLR via C # and C # in Depth, 3rd Edition

1. The method of Origin

① method (method), formerly known as C / C ++ language function (function)

The method is the concept of object-oriented areas, still called a function in non-object-oriented languages.

You can use C / C ++ language do comparison.

* When the function appears as a member of our call it method (starts with C ++)

② always a class (or structure) of the members

It can not function in the C # language (or outside a structure), independent of class.

Only members of a class (or structure) when the method is called.

In C ++, it is called "Global Functions."

③ One is the class (structure) of the basic member

Only two members of the most basic - fields and methods (member variables and member methods), or the nature of the data + algorithm

The method represents a class (or structure) "can do anything."

④ Why do we need methods and functions

Objective 1: hide the complexity of the structure;

Objective 2: multiplexing (resue, reuse);

C++中#include

#include "Student.h" // If this is the standard library, then use angle brackets, if you define it in quotation marks.

2. The method of statement calling

Detailed method statement syntax ①

See the C # language documents (declarations / definitions, regardless of family);

Parameter called the "formal parameter";

Formal parameter, referred to as "parameter": logic configured in the form of parameters involved in the algorithm Algorithm (i.e., in the algorithm as variables involved in the calculation);

Parameter is a variable: variable parameter form;

② method naming convention:

Case specification: pascal law (capitalize the first letter of each word);

It needs to be a verb or verb phrase as the name;

③ review static (static) and instance methods: the static method is part of the class, the target class can not be called; instance method (non-static method) belonging to an object class, the object can be called.

④ call the method: the method name behind the parentheses can not be omitted.

Function declaration "()" which is a parameter; function call "()" which is the argument.

The official Chinese translation of Argument C # document as "actual parameter" the "argument" is understood the true condition of the calling method.

Argument List when calling the method with the parameter list to match the time of the definition of the method, C # is a strongly typed language, argument is a value, a variable parameter, the value of the variable (the number and data type) or will be error must match.

3. Constructor

Constructor (constructor) is a member of the type;

Narrow constructor means "instance constructor" (instance constractor);

How to call the constructor;

Statement constructor;

Memory principle constructor;

Constructor is an action instance (object) and to initialize the class constructor.

① When you call the default constructor:

The statement: Student stu = new Student (); ①

Class Student

{

Public int ID;

Public string Name;

}

① Since * statement is a main function, the STU reference variable is local, the first 4 bytes allocated free memory in the stack; * new new operator to create an instance of the class Student, by calling Student (); default constructors, and opened in the heap memory size of a memory space included in the data storage space for the instance instance, (int ID; 4 bytes, string name4 bytes) so allocated memory space of 8 bytes , because the default constructor implicitly assigned a 0, so the block of data in memory the initial value 0; * Finally, the first address memory into binary numbers, copied to the stack memory reference variable among stu.

② When you call the constructor has parameters:

First and third steps the same operations and when calling the default constructor; except that the second step:

When parameter & numeric type: in the first 8 bytes of this memory heap memory space instance, int type 4 bytes, is passed in argument 1 (assumed) put 1 (to be converted to binary) into the presence of the binary memory;

When & parameter reference type (class type are usually remember reference types): The remaining 4 bytes of storage space is a string type, (assuming is passed in argument "it'OK")

Since the string is a class type, and class type is a reference type, so this four byte stored inside the only reference variables, but also go to the heap to find a location to store the argument "it'OK". After finding memory space to "it'sOK" string data into binary data into them. (The last memory address into a first binary string type stack memory exists in accordance with the above)

The method of overload (Overload)

① of example overloaded method call;

② declaration with overloaded methods

The method signature (method signature), type parameter (generic speaking when encountered) the name of the method and the number of each of its formal parameters (order left to right) type and kind (value, reference (ref) or output (OUT)) composition. Method signature does not contain a return value. (Mainly to see the method name and parameter list)

Instance constructor signed by each of its formal parameters (order left to right) type and kind (value, reference, or output) composition;

Overload resolution (which in the end an overloaded call): used in the case given the list of parameters and a set of candidate function members, select a member to implement the best function call.

5. The method of how to debug (core skills)

Set a breakpoint (Breakpoint);

call stack when viewed method invocation;

Step-in,Step-over,Step-out;

Observation and the change in local variables. (Local variables can be viewed through the window, the mouse can also be viewed on the variable, the mouse may be placed in a small window on the fixed variable to observe)

The relationship between the call stack (call stack) and the stack memory: for example, recursive, constantly calls itself until there is a stop value, if not, the call stack will be deeper and deeper until the memory stack accounted burst, this time stackOverflow (stack overflow)

Step-in (F11) (Step Into): entry method, the most delicate debug tool

Step-over (F10) (Step Over): Do not jump directly into the method, relatively rough some debug tools

Use to combine both general, there is a problem with the F10 is the first rapid investigation of a wide range of methods which use F11 again into the method step by step debug. (Suspected bug place on the F11 careful investigation)

Step-out (jump): the role of the class or method to jump to the inside (in order) the breakpoint call to the current method calls, for example, 1 2,2 3 calls, then set a breakpoint in a first hop press Shift F11 to at 2, and then a jump to 3.

* Integrated use of these three methods will make us a powerful debug capability.

6. The method of calling and Stack

Stack memory allocation method is called when ①

Analysis of the stack frame (a method of layout in the stack called procedure) of

* After the stack memory bit low byte high byte bits developed by the development to a minimum will Overflow,

For example, calling the main function output statements in C # fun turn three methods, when invoked, main method to open a memory called stack frame (stack frame) in the stack memory.

Call relations approach are calling and calling, caller called the Caller, called the callee Callee, then the question is the main function such as call other functions: Console.WriteLine (c.GetConeVolume (100, 100)); then pass in the two parameters (100) which function to manage it, different regulations in different languages, C # is based on standard C ++, parameters are owned by the caller (caller) tube, here is the main pipe. In the stack memory, respectively, open space the size of the corresponding type of parameter, which parameter be pressed into it? Different languages ​​prescribed different, C # follow the rules of C ++ first left and right, who calls who is in charge of the parameters pressed into (stack frame is the main function of) the stack so the film memory is a main tube, * are the first that is the method runtime open memory stack frame within the stack and then the data within the method comprising the sheet are pressed into the stack memory. Now called method, follow the principle of who is calling who push, even without a local variable within the method which will be a stack frame (storage method returns the value of the address, etc.), the return value of the method is the presence of cpu registers (when school compilation there are mentioned) inside.

* Results after completion of a method for operating a return value, a corresponding stack frame (stack frame) will be emptied, the end of the respective functions (methods) without using the call parameters in the memory stack frame which will be cleared . (Lenovo can push and pop compilation language)

* This may also explain the overflow, because abnormal method has been called, has been

Open up the stack frame, no layers of return, the corresponding parameters not only push the stack until the top of the stack to the open space caused by "stack overflow" This is the principle of stack overflow.

Guess you like

Origin blog.csdn.net/qq_43425487/article/details/102752756