Inside C # (some internal implementations)

Source: Internet
Author: User
Ildasm is a good tool that I like very much at ordinary times. It can help me better understand net. In my summary, please teach.

You write:

Public Enum compression {

Zip, superzip, none

}

The C # compiler generates:

Public struct compression: Enum {

Public int value __;

Public const compression zip = 0;

Public const compression superzip = 1;

Public const compression none = 2;

}
What can we learn:

1)Enums is struct, and the internal Member is struct and enum.
2) Enum inherits from system. enum. structs can inherit from valuetype or enum.
3) The Enum internal Member is int by default.
4) variables containing "_" cannot be used.
5) All values are const, so they cannot be changed without re-compilation, and are also type-safe.
 

You write:

Public class Resource

{

~ Resource (){

...

}

}

The C # compiler generates:

Public class Resource

{

Protected override void finalize (){

Try {

...

}

Finally {

Base. Finalize ();

}

}

}

What can we learn:
1) The parsing function overrides the Finalize of the base class. And it is uncertain.
2) method (represented by the "…") Put it in try. The finalize of the base class is called no matter whether there is any exception.

You write:

Using (resource res = new resource ()){

Res. dowork ();

}

The C # compiler generates:

Resource res = new resource (...);

Try {

Res. dowork ();

}

Finally {

If (res! = NULL)
(Idisposable) RES). Dispose ();

}

What can we learn:

UseUsingWill be compiled into try, and will always execute the dispose method (CLR-based exception ).

You write:

Object x = ...;

Lock (x ){

// Critical section

}

The C # compiler generates:

System. Threading. Monitor. Enter (X );
Try {
// Critical section
}
Finally {
System. Threading. Monitor. Exit (X );
}

What can we learn:
1)Lock just uses monitor. enter and exit... Reading those docs give you a good idea of what is really happening here.
2)We see our friend try... finally again. This time ensuring that the monitor is exited even if an exception is thrown in the critical section

You write:
Arraylist list = new arraylist ();
Foreach (int x in list) {// do nothing}

The C # compiler generates:
Arraylist list1;
Int num1;
Ienumerator enumerator1;
Idisposable disposable1;
List1 = new arraylist ();
Enumerator1 = list1.getenumerator ();
Try {
While (enumerator1.movenext ()){
Num1 = (INT) enumerator1.current );
}
Return;
}
Finally {
Disposable1 = (enumerator1 as idisposable );
If (disposable1! = NULL ){
Disposable1.dispose ();
}
}

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.