Anders on C # 4.0: New features and prospects

Source: Internet
Author: User
Tags ruby on rails learn ruby on rails

Just read the Anders PDC Talk, I feel the change of C # 4.0 is not very big, but many changes are convenient for users, some even the original Anders did not want to join the function finally because the user's voice finally added (such as Optional Parameters), can be seen Anders to the user's opinion is very important. To sum up, the following features are available in C # 4.0:

1. Dynamic/idynamicobject

This improvement makes C # a step further into the dynamic language, although C # does not become a dynamic language such as Perl/python (because Anders believes that some of the features supported by static languages such as Intellisense, type checking, etc. are quite useful), but this does not mean that C # You should not provide better support for dynamic features. From the point of view of our interop, dynamic is similar to the IDispatch in COM, which dynamically chooses the matching action based on the provided function/attribute name, except that the interface now becomes IDynamicObject. The wording is similar to VB6.

Originally to write:

   1:object obj = GetObject ();
   2:obj. GetType (). InvokeMember ("Callsomefunc", ..., new object[] {1});

Now just:

   1:dynamic obj = GetObject ();
   2:obj. Callsomefunc (1); OBJ supports Callsomefunc methods through the IDynamicObject interface.                     

All this is done through the IDynamicObject interface. As long as the object supports IDynamicObject, then any object can be invoked directly in this way, whether it be com,python,javascript, and so on. This function is basically to define an interface, and then the compiler to translate the code is good, the key is the support of various objects.

2. Optional parameter/named Parameters

Optional parameters that were previously not supported by C # are finally supported. Named arguments can also be supported by using a parameter plus a colon:

   1:opentextfile ("Foo.txt", Encoding.UTF8, buffersize:123)

3. Improved COM interoperability

3.1 Automatic object–> dynamic mapping

Where the object was originally returned, object can now be automatically considered dynamic. Therefore, the place where the cast was previously required can now save the cast, anyway the dynamic object can indirectly invoke the IDispatch interface via IDynamicObject (I think it should still be through Memberinfo.invoke. Call IDispatch, but temporarily does not have time to verify its implementation) to automatically invoke the corresponding function, and do not need to cast to the corresponding interface call again.

The original is: (Range) Excel. Cells[1, 1]. Value = xxx;

Now you can write: Excel. cells[1,1]. Value = xxx; Call Idynamicobject.setmember ("Value", XXX);

3.2 Optional and named parameters

There's no need to say more about it. BTW, now TlbImp's results (ie, interop Assembly) already contain the default values in metadata, but C # is not necessary, and C # can now be used directly.

3.3 Indexed Property

This Anders a sentence with a word, temporarily not clear what is specific improvement.

3.4 Optional ref modifier

You do not have to write ref when COM interop. The concrete Anders also did not talk much. Think it should be a small change.

3.5 Interop Type Embedding (NO PIA)

This is the type equvalency I mentioned earlier in the previous article. In order to ensure that the same COM interface has the same managed type (because there can be multiple corresponding managed interfaces for the same COM interface), the PIA (Primary Interop Assembly) is recommended. However, in the process of using the PIA, there are a number of problems with the PIA, so one of the architect of the CLR interop has come up with this new idea: instead of using a PIA, you can allow interchangeably between different managed interfaces that correspond to the same COM interface, without Cast, Within the CLR, they are treated as equivalent. This is a relatively large change, whether for the compiler or the CLR. More details I will be in the future blog in a number of detailed introduction. (Update: My first article about this new feature:. NET 4.0:type Equivalency (1) –byebye,pia)

4. Co-variance & Contra-variance

This is relatively difficult to understand. Co-variance and Contra-variance in this series of articles are described: http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+ Contravariance/default.aspx, this person is the C # compiler Dev, naturally has a more authoritative explanation, and this series has n articles, speaking of more complex. I will explain this piece of content in a blog in detail when I am free.

In simple terms, co-variant indicates that a template parameter is used as outgoing, that is, the return value of a function or out parameter, and so on, with the keyword out:

   1:public interface Ienumerator<out t>//Co-variant
   2: {
   3:     T current {get;}
   4:     bool Next ();
   5:}
   
   7:ienumerator<string> strings = Getstrings ();
   8:ienumerator<object> objects = strings; This OK, conversely error

The conversion in the above means that IEnumerator can be regarded as IEnumerator

Conversely, contra-variant indicates that the template parameter T can only be used in a function entry parameter or a property's entry, in the expression:

   1:public interface Icomparer<in t>//Contra-variant
   2: {
   3:     int Compare (t x, t y);
   4:}
   
   6:icomparer<object> Objcomp = Getcomparer ();
   7:icomparer<string> StrComp = Objcomp; This OK, conversely error

Similarly, any place that uses Icomparer.compare (string x, string y) will be passed into a string and will naturally be IComparer

5. C # 5.0???

5.1 meta-programming Capabilities

Learn Ruby on Rails to introduce powerful meta-programming capabilities.

5.2 Compiler as Service

Rewrite the compiler with managed code (originally written in C + +, using the IMetaDataEmit interface to output the PE file), supporting others to write code to participate in the entire compilation process (estimating similar plugin) or call the compilation process (similar to the Eval function of an existing dynamic language).

Anders demonstrates the direct compilation of C # code in string by Csharpevaluator and then executes directly:

   1:csharpevaluator ev = new Csharpevaluator ();
   2:ev. Eval ("for" (int i = 0; I < 10; i++) {...} ");

This makes it easy to write a C # Shell:

c#> int Add (int a, int b) {return a+b;}

C#> Add (1, 2)

3

c#> form = new form () {Text = "Hello World"};

This is basically the original dynamic language strengths, now C # can also be done.

Interested friends can find this talk:http://channel9.msdn.com/pdc2008/tl16/below. Both online and downloadable.


Turn from: http://blog.csdn.net/ATField/archive/2008/11/01/3203602.aspx

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.