Similar to C # API value type and Reference Type Static non-static Exception Handling GC garbage collection value type reference type memory allocation <4>,
C # basic interface
I. multi-State review
New is used for implementation, and virtual and override are used.
-- New hides the parent method based on the current type and electrophoresis method (member)
-- Override rewrite is used to execute new methods (Members) under any circumstances)
Inheritance is a prerequisite for realizing polymorphism. It cannot be implemented without inheritance polymorphism.
Implementation of polymorphism between parent class and subclass
Abstract class and subclass implementation
Abstract classes cannot be instantiated.
The abstract method in the abstract class does not have a method body.
What are the members of an abstract class?
-Contains non-Abstract members
-"Cannot be instantiated.
-The subclass must implement the abstract method of the parent class, unless the subclass is also an abstract class
What are abstract members? (All methods can be abstracted)
Methods, attributes, indexes, and events
Automatic attributes and abstract attributes
This field can be used only when the Read and Write Functions are implemented and no other control is required.
Ii. Interfaces
1) interface Keyword: interface
2) the interface name starts with I
3) the member has no access modifier.
4) there is no way to implement the body
5) the members in the interface must be abstract.
6) The interface definition capability means that the method derived class must implement the interface method, except the abstract class
7) the interface exists to realize polymorphism.
8) inheritance can solve the problem of huge inheritance volumes. For example, there is an algorithm class with many functions and complete computing methods. Now I want to implement 1 + 1 = 2 computing, at this time, if you call this fully functional class library for implementation, it will undoubtedly make the big data useless.
There will also be a lot of redundant code, so you can consider using interfaces for implementation. The single interface principle, the fewer functions the interface implements, the better. Generally, an interface implements a function, generally, there are no more than two methods in the interface.
Interface can implement multiple inheritance:
Syntax:
[Public] [static] class name: [base class name,] [[interface name,] interface name...]
Interfaces are more abstract than abstract classes. interfaces are abstract functions.
Display implementation interface:
-To avoid the same interface method name
-Explicitly Implemented interfaces must be called by the interface type
The method used to implement the interface cannot be modified as public. The method name must be the interface name. Method Name
Interface IInterface1 {void Func ();} interface IInterface2 {void Func ();} class MyTest: IInterface1, IInterface2 {void IInterface1.Func () {Console. writeLine ("I am the Func provided by IInterface1");} void IInterface2.Func () {Console. writeLine ("I am the Func provided by IInterface2 ");}}
Add code segment
C # value type_reference type
Iii. Value Type and reference type
The value type is "copy file"
-The value Type is from Vluar Type.
The reference type is "Copy shortcut"
-> The reference type is from object.
You can use both ref and out to pass the parameter into the method, and retain the influence on the parameter value assignment in the method.
That is, the parameter is modified in the method. After the method is completed, the last modified value in the method is retained.
The ref reference method uses a variable reference as a shortcut.
The Out parameter is used for output, that is, the modification to the variable in the method is to be passed to the output
Ref must be assigned a value before use.
The out value must be assigned before being used in the method.
static void Main(string[] args){int num = 0;Func1(ref num);int num1;Func2(out num1);}static void Func1(ref int n){n = 10;}static void Func2(out int n){n = 10;}
C # static and non-static
Try {// code that may cause an exception // open the database, operate on data, etc // once an exception occurs, stop from the exception and jump to the catch, the subsequent code in tyr will no longer execute} catch (Exception ex) {// once an Exception occurs, execute the code here // generally logs} finally {// release resources}
An exception is thrown up. If the above (caller) is not caught, it will be thrown up. If it is not caught, it will be thrown to the system, the system will retrieve this exception and provide a solution
If the system cannot solve the problem, it will be thrown to Microsoft.
Whether an exception is thrown or return finally is always executed. (try not to use try-catch for errors that can be judged by using if)
C # _ GC garbage collection
Iii. GC garbage collection
Garbage collection starts from 0th generations.
Every time garbage collection is performed, if the previous generation is not used, it will be cleared,
If some objects in the previous generation are still in use, they will be promoted to the next generation.
How to automatically release resources
-Object in. net has the concept of generation, and each generation has a memory range.
-". Net has three levels: 0, 1, and 2.
-2-generation maximum memory
-"The default value of each created object is generation 0. When the object reaches generation 0, it will automatically trigger the reclaim of generation 0th.
-"Recycle" means to move the objects to be used to another continuous memory.
-All address points will change (this movement is sometimes called compression)
-In this case, when the first generation is full, it will call to reclaim the first generation and put the objects to be used in the second generation.
-When the second generation is full, an overflow exception will be thrown.
When the number of bytes of an object exceeds 85000, the object is stored in the large object hosting heap.
During garbage collection, data in the large object area is not considered (unless the data in the large object area is smaller than 85000)
21250 int type objects = 85000 bytes
// 16
MyClass n1 = new MyClass ();
MyClass n2 = new MyClass ();
MyClass n3 = new MyClass ();
MyClass n4 = new MyClass ();
MyClass temp = n2;
N2 = null;
// 0x0012
// Manually call the Garbage Collector
System. GC. Collect (0 );
// It will be optimized based on the memory usage in your current system
N1 = null;
System. GC. Collect (0 );
System. GC. Collect (1 );
Console. ReadKey ();
Static void Main () {int num = 10 ;}
The code above will open up a thread stack when the program runs into the Main release system. This thread stack is used to store the code executed by this method,
The program will retrieve all the variables in the method at the beginning, and then open a memory for int num. The system will clear the data in the memory by 0x0000,
And give the memory space an address (equivalent to a number for the memory space ).
Value-type variables exist directly in the thread stack, and the Values exist in the thread stack. When the data is declared, the system will open up memory space of the corresponding size according to the declared type.
3. Reference Type
-Reference types are divided into two types of space for storage, variable storage in the online process stack, object storage in the managed Stack
class Person() { } static void Main() { Person p=new Person(); }
At first, the program opened a memory in the stack space to clear the data (0x0000), and then stored the p in the stack space, when the program runs in the main method, it will first execute new Person (); (that is, in the new object)
In this case, a space will be opened in the heap space to clear the data, allocate fields, initialize the constructor, and generate an address. An address is returned when a new object is created, this address points to this object, and then executes the equal sign assignment. At this time, the assignment assigns the address generated by this object in the heap space to the variable p. In this case, the variable P Stores the address.
We actually process this address.
So when we pass the parameter, we copy the value in the variable and pass it over. This address is copied and assigned to the variable received in the method, at this time, this variable is used to process the address in the method, so it will be affected outside
Ii. Object creation (a lot of work is actually done in the memory when the new object is created)
-- Searches for the inheritance relationship of the class and calculates the number of bytes of all fields from top to bottom.
-- Add this number to 8 bytes (if it is a 64-bit operating system plus 16) (here, the 8-byte method stack occupies 4 pointers)
-- And then apply for space from the Operating System
-- If the space is full, an overflow exception is returned.
-- If the space is sufficient, the first address of the space will be returned.
-- "Clears the memory space of this shard based on the calculated length.
-- Nextobjptr adds the computed data to the original address location to obtain the first address of the next object.
-- Call the constructor
Multiple objects are stored continuously in the memory.