Beginner with c #2

Source: Internet
Author: User

1. 2. Automated memory management)
Manual memory management requires programmers to allocate and release memory blocks themselves. This requires the programmer to have a clear mind and a very good understanding of the entire operation process.
GRASP (difficult !). C # frees programmers from this difficult task. In most cases
High code quality and programmer productivity. In addition, it will not affect the program's intention and execution (? I don't believe m $'s ghost
). However, it is better than the java recycle bin. Because c # is too late to come out ). Let's take a look at the example. */

Using System;
Public class Stack
{
Private Node first = null;
Public bool Empty {
Get {
Return (first = null );
}
}
Public object Pop (){
If (first = null)
Throw new Exception ("Cant Pop from an empty Stack .");
Else {
Object temp = first. Value;
First = first. Next;
Return temp;
}
}
Public void Push (object o ){
First = new Node (o, first );
}
Class Node
{
Public Node Next;
Public object Value;
Public Node (object value): this (value, null ){}
Public Node (object value, Node next ){
Next = next;
Value = value;
}
}
}

Class Test
{
Static void Main (){
Stack s = new Stack ();
For (int I = 0; I <10; I ++)
S. Push (I );
While (! S. Empty)
Console. WriteLine (s. Pop ());
}
}
/*
Stack class implements a series of Node instances. Let's take a look at the stack Push method. Node instances are created in the Push method.
That is, "first = new Node (o, first );". Remember this "new. It is used to create class instances. The related syntax is too
More, take a look at it later. What is the benefit of Automatic memory management ?! "New"
Is responsible for class instance initialization. To release these instances in c/c ++, use another keyword "delete ". But when should I use delete,
This is usually a very difficult job, and the veteran is also turning over the ship in the perineum. What's more, it's me! But it is not used in c. In this example, "delete" is not used ".
When a Node instance is not needed, the garbage collector automatically destroys it. This is similar to java.
Image (may be copied ).

In a test class, I used a loop and assigned ten values to the Push method of the stack class instance. Therefore, Push creates ten instances of Node.
Instance ). Then, use Pop to display them. The order is the opposite of the order of creation.
This example is quite good. It is stack.
Is also a typical expression of the automatic memory management mechanism. But it is hard to understand. Fortunately, this section is not intended for users without any foundation.
It took me a few minutes to understand that it was okay for all the prawns.

In fact, after "10" is displayed, a Node instance will meet the conditions for release, but the garbage collector does not necessarily do this.
That is to say, its behavior is not sure (this is the same as java, I guess ). Sometimes, this behavior has some negative effects. At least sex
Can be reduced. Automatic Memory Management is also a problem. It is difficult to manage some special cases. There are some java garbage collectors
This article also mentions. M $ is not much better. Therefore, m $ has an Insecure code term (unsafe code) to serve advanced users.
Service. That is, you can choose not to use the garbage collector. However, it must be explicitly declared with the "unsafe" keyword. In this way, you can avoid
Use Insecure code. The following is an example :*/

Using System;
Class Test
{
Unsafe static void WriteLocations (byte [] arr ){
Fixed (byte * p_arr = arr ){
Byte * p_elem = p_arr;
For (int I = 0; I <arr. Length; I ++ ){
Byte value = * p_elem;
String addr = int. Format (int) p_elem, "X ");
Console. WriteLine ("arr [{0}] at 0x {1} is {2}", I, addr, value );
P_elem ++;
}
}
}
Static void Main (){
Byte [] arr = new byte [] {1, 2, 3, 4, 5 };
WriteLocations (arr );
}
}
/*
I am not very satisfied with this example, and I am also confused. I have the opportunity to write another one myself. It's easy, but you can use pointers! Long live!
In fact, I am not sure about this section! There are many places that cannot justify themselves! Therefore, please criticize it. */

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.