Chila C # Interface value type vs. reference type static non-static exception handling GC garbage collection value type reference type memory allocation < four >

Source: Internet
Author: User

C # Basic Interface Chapter

I. Polymorphism Review
Use a new to implement, using virtual and override
--"New hidden Parent" method according to the current type, electrophoresis method (member)
--"Override overrides whatever the case, is to execute the new method (member)

Inheritance is a precondition for polymorphism, and no inheritance polymorphism is impossible.
The parent class and the subclass implement polymorphism
Abstract class and subclass implementation
Abstract classes cannot be instantiated
Abstract methods in abstract classes have no 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? (anything that is relevant to a method can be abstracted)
Methods, properties, indexes, events
Automatic attributes and Abstract properties
This field allows you to use automatic properties when you need no additional control for the function of reading and writing.

Second, the interface
1) interface Keyword: interface
2) The name of the interface begins with I
3) member character does not have access modifier
4) There is no way to implement the body
5) The members in the interface must be abstract
6) Interface definition capability that method derived classes must implement interface methods, in addition to abstract classes
7) The presence of the interface is to achieve polymorphic
8) Inheritance can solve the large inheritance of its problems, such as there is an algorithm class inside there are many functions of the calculation method, I now want to implement 1+1=2 calculation, at this time if called this full-featured class library to achieve, then undoubtedly the overqualified
will also produce a lot of redundant code, then you can consider the interface to implement the single principle of the interface, the less the interface implementation of the better, generally an interface to achieve a function, the general interface does not exceed two methods

Interfaces can implement multiple inheritance:
Grammar:

[Public] [Static class name: [base name,][[interface name,] Interface name ....]

Interfaces are more abstract than abstract classes, and interfaces are abstractions of functionality.

Display Implementation interface:
-"To avoid the same method name for the interface
-the explicit implementation of an interface must be called by an interface type
The method that implements the interface cannot be decorated with the interface name. Method Name

InterfaceIInterface1 {voidFunc (); }    InterfaceIInterface2 {voidFunc (); }    classMytest:iinterface1, IInterface2 {voidIinterface1.func () {Console.WriteLine ("I am the IInterface1 provided by the Func"); }        voidIinterface2.func () {Console.WriteLine ("I am the IInterface2 provided by the Func"); }    }

Add code Snippet

C # Value type _ reference type Chapter

Three, value types, reference types
Value types are "copying files"
-"Value type from Vluar type"
Reference type is "copy Shortcut"
The reference type is derived from Object
Use ref and out to pass parameters to methods and preserve the effect of assigning values to parameters in a method
That is, the parameter is modified in the method, and after the method is finished, the result still retains the last modified value in the method.
A reference to a variable in the Ref reference reference method is a shortcut
The out parameter is used to output, that is, the modification of the variable in the method is to be passed to the outside output
Ref is to be assigned before use
The value to be assigned before the out is used in the method

Static voidMain (string[] args) {intnum =0; FUNC1 (refnum);intNUM1; FUNC2 ( outnum1);}Static voidFUNC1 (ref intN) {N=Ten;}Static voidFUNC2 ( out intN) {N=Ten;}

C # static and non-static articles

Third, static and non-static
Static members are made members of static modifiers
Static members act on the entire application
-"It can be called in the program."

How do I define a static member?
-"plus static modifier"

A static member invokes a relationship with an instance member?
-"Static methods, static classes are not allowed to call static members directly, if you want to call the new object must be called by the object
How do I invoke a static member?
-"class name". Member name

Static members belong to the entire class

Instance members belong to an object

This class can be defined as a static class when a member of a class is a static member and is not instantiated, and is reused in the program.
Static is the same as the abstract level
-"can only be modified by one of the

Not related to public and internal

Static classes do not allow inheritance
Instance members are not allowed inside static classes

Static Construction method
-"No Parameters allowed"
-"only allowed by static decoration, no public decoration, etc.

The static construction method is executed only once from the beginning of the program to the end (the class will be the first to execute a static method)

Dotnet Basic _ Exception handling article

Iv. Handling of anomalies
1) Try-catch
2) try-catch-finally
3) tre-finally

Try   {     // Possible abnormal amount of code   // Open Database, operation data, etc.   / /  Once an exception occurs, it stops at the exception to jump to the catch, and the subsequent code in the Tyr no longer executes the   }  catch(Exception ex)  {       // Once an exception occurs, execute the code   here // usually do log etc.   }  finally  {    //  frees resources  }

The exception is thrown upward, if the above (the caller) does not catch, then it will be thrown to the top, if the top has not been caught on the system will be thrown, the system will retrieve this exception, give a solution
If the system doesn't work, it will be thrown to Microsoft.
Whether throwing an exception or return finally will always be executed, (you can use if to judge the error try not to use Try-catch)

C#_GC Garbage Collection article

Third, GC garbage collection
Garbage collection always starts from generation No. 0
Every time a garbage collection is done, the previous generation will be cleared off if it is not used.
If, in the previous generation, some objects are still in use, they are promoted to the next generation.

How to automatically release resources
-". NET objects have a generational concept, and each generation is a memory-scoped
-The generation in. NET has 0, 1, 23 levels
-"2 generation of memory maximum
-"The object created by default is 0 generations, and when the object reaches 0, it will automatically trigger the collection of the No. 0 generation.
-"recycling" is actually moving objects that need to be used uniformly to another contiguous memory
-"All address points are changed (this movement is sometimes called compression)
-"When the first generation is full, the first generation is called, and the objects that need to be used are placed in the second generation.
-"When the second generation is full, a overflow exception is thrown
When the number of object bytes is greater than 85000, the object is stored in the large object managed heap
In the case of garbage collection, do not consider the data of the large object area (unless the data of the large object area is less than 85000)
21,250 objects of type int ==85000 bytes

16 x
MyClass n1 = new MyClass ();
MyClass n2 = new MyClass ();
MyClass n3 = new MyClass ();
MyClass N4 = new MyClass ();

MyClass temp = n2;

N2 = null;
0x0012
Call the garbage collector manually
System.GC.Collect (0);
Optimized based on memory usage in your current system
N1 = null;
System.GC.Collect (0);
System.GC.Collect (1);
Console.readkey ();

C # value type reference type memory allocation Chapter

Review value types and reference types
The reference type is the address that is processed
--"Shortcut to file relationship"
--"What did the object do in memory?"
A 32-bit computer represents an integer with 32 2 binary bits, while using 32 bits to describe an address

1, the allocation of memory space: divided into 3 space
In the stack space, the system allocates memory space from high to low (that is, from a large number to a small number).
In the heap space, the system allocates memory space at low to high (that is, from small to large)

1) A memory space to store the code in the current execution method
-"Call Thread Stack"
-"A method of a thread stack

The program executes at the beginning to retrieve what variables are in each method, and allocates memory space for these variables, which is equivalent to opening a good opinion of the empty room to the room number (that is, the address),
And then the memory of the data 0 (note that the 0 is equivalent to the data into such a 0x0000, so the program does not execute to this variable, the cursor is placed on this variable will see the value of this variable is 0,
However, the variable is not assigned at this time, it is not available, because the 0 is the 0x0000 of the data in the memory space, not the assignment, and so on after the equal sign is executed to really assign a value to this variable.

2) A space for storing objects
-"Managed heap"
An object is the value of a variable, such as an int num=10;   New Person (); This 10 and new person () is the object.

3) storing static objects, large objects (greater than 85000 bytes), etc.

2. Value type:

Static void Main ()  {       int num=;  }

The above code, the program execution to the main system will open a line stacks, this line stacks to hold the code to execute this method,
The program retrieves all the variables in the method from the beginning, and then opens up a memory for int num, which clears the data from the 0x0000.
and give the memory space an address (equivalent to a number of this memory space).

A variable of a value type exists directly in the thread stack, where the value also has a line stacks. When the data is declared, the system will open up the corresponding size of memory space according to the declared type.
3. Reference type
-"Reference type is divided into two spaces to store, variables stored in the thread stack, objects stored in the managed heap

Classstaticvoid  Main () {person    p=new person ();}

The program starts with a memory in the stack space to clear 0 (0x0000), and then put this p into the stack space, when the program executes into the main method, the new person will be executed first ();(is the new object)
At this point in the heap space will open up a space to clear the data, and then assign the field, the construction method is initialized, and generate an address, the new object returns an address, the address of this object, and then execute an equal value assignment, when the assignment is the object in the heap space generated by the address assigned to the variable p. In this case, the address is stored in the variable p
We are dealing with this address in fact.
So we are in the time to pass the argument is to copy the value of the variable in the past, that is, to copy the address of an assigned method to receive the variable, at this time in the method of the variable is the address of the processing, so will be outside the influence

Second, the creation of the object (when the new object is actually doing a lot of things in memory)
---Search for the inheritance of the class, counting the bytes of all the fields from the top down
--"Add this number to 8 bytes (if 64-bit OS plus 16) (here 8 bytes method Stack 4 pointers accounted for 4)
--"then apply for space to the operating system
--"If the space is full, return an overflow exception
--"If there is enough space, it will return to the first address of the space.
--"according to the calculated length, this piece of memory space will be zeroed out
--"nextobjptr will add the data just calculated at the original address and get the first address of the next object."
--"Call the constructor method

Multiple objects are stored continuously in memory.

Chila C # Interface value type vs. reference type static non-static exception handling GC garbage collection value type reference type memory allocation < four >

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.