About C #4.0: New features and prospects

Source: Internet
Author: User

After reading the PDC talk of Anders just now, I feel C #
4.0 of the changes are not great, but many changes make it easier for users to use. Some of the features that Anders was not willing to add finally were added due to user voices (for example
Optional parameters), it can be seen that Anders attaches great importance to users' opinions. To sum up, C #4.0 has the following features:

1.Dynamic/idynamicobject

This
This makes C # Another step toward dynamic language, although C # won't become a dynamic language like Perl/Python (Because Anders thinks that static languages support some features such
Intelliisense, type check, and so on), but this does not mean that C # should not provide better support for dynamic features. From our InterOP perspective
See, dynamic is similar to the idispatch in COM, that is, dynamically selects and executes the matching action based on the provided function/attribute name, but this interface has now become
Idynamicobject. Similar to VB6.

Originally to write:

   1: object obj = GetObject();
   2: obj.GetType().InvokeMember(“CallSomeFunc”, …., new object[] { 1 });

Now you only need:

   1: dynamic obj = GetObject();
2: obj. callsomefunc (1); // OBJ supports the callsomefunc method through the idynamicobject interface.

All of this is implemented through the idynamicobject interface. As long as the object supports idynamicobject, any object can be called directly in this way,
Com, Python, JavaScript, and so on. This function basically defines an interface, and then the compiler translates the code. The key is the definition of various objects.
Yes.

2. optional parameter/named Parameters

Optional parameters that were previously not supported by C # are now supported. The naming parameter can also be supported. Use the parameter with a colon:

   1: OpenTextFile(“foo.txt”, Encoding.UTF8, bufferSize:123)

 3. Improved com interoperability

3.1Automatic Object-> dynamic mapping

Where the original object is returned, the object can now be automatically regarded as dynamic. Therefore, you can save cast for the places where cast was previously needed.
Dynamic Objects can indirectly call the idispatch interface through idynamicobject (I think we should still use memberinfo. Invoke)
Call idispatch, but there is no time to verify its implementation method) to automatically call the corresponding function, without the need to cast the corresponding interface and then call.

Originally: (range) excel. cells [1, 1]. value = xxx;

Now you can write it as Excel. cells []. value = xxx; // call idynamicobject. setmember ("value", XXX );

3.2Optional and named Parameters

You don't need to talk about this. BTW. Now, the tlbimp result (InterOP assembly) already contains the default value in metadata, but C # doesn't need it. Now C # can be used directly.

3.3 indexed Property

This Anders has been mentioned in a single sentence, so it is unclear what improvements are made.

3.4Optional ref Modifier

You do not need to write ref in COM InterOP. I have not talked about the specific Anders. I think it should be a small change.

3.5 InterOP type embedding (no PIA)

This is the type I mentioned in the previous article.
Equvalency. To ensure that the same COM interface has the same hosting type (because the same COM interface can have multiple hosted interfaces), we recommend that you use
Pia (primary InterOP assembly ). However, when Pia is used, it is found that Pia has many problems, so CLR
A cool of InterOP called fig came up with the new idea: instead of using Pia, it allows different hosted interfaces corresponding to the same COM interface to be used interchangeably without having
Cast and CLR are equivalent to each other. This is a big change, whether for the compiler or CLR. For more details, I will introduce them several times in my blog
Shao. (Update: My first article about this new feature:. Net 4.0: Type equivalency (1)-Byebye, PIA)

4. Co-variance & contra-variance

This is hard to understand. Co-variance and contra-variance are described in this series: variance. If you are free, I will explain this content in detail in the blog.

In short, co-variant indicates that a template parameter is used as the output parameter, that is, the return value or out parameter of the function, and so on. The keyword out indicates:

   1: public interface IEnumerator<out T> // Co-Variant
   2: {
   3:     T Current { get; }
   4:     bool Next();
   5: }
   6:  
   7: IEnumerator<string> strings = GetStrings();
8: ienumerator <Object> objects = strings; // This OK is returned.

The preceding conversion means that ienumerator Can be considered as ienumerator

On the contrary, contra-variant indicates that the template parameter t can only be used in function input parameters or attribute input parameters, and in indicates:

   1: public interface IComparer<in T> // Contra-Variant
   2: {
   3:     int Compare(T x, T y);
   4: }
   5:  
   6: IComparer<object> objComp = GetComparer();
7: icomparer <string> strcomp = objcomp; // This OK is returned.

Similarly, any use of icomparer . Compare (string X, string y) will be passed in string, which will naturally be used by icomparer

5. C #5.0 ???

5.1 meta-programming capabilities

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

5.2 compiler as service

Rewrite the compiler with managed code (originally written in C ++ and output the PE file using the imetadataemit Interface), and support others to write code to participate in the entire compilation process (it is estimated that it is similar to plugin) or call the compilation process (similar to the eval function of an existing Dynamic Language ).

Anders demonstrates using csharpevaluator to directly compile C # Code represented by string and then directly execute:

   1: CSharpEvaluator ev = new CSharpEvaluator();
   2: ev.Eval(“for (int i = 0; i < 10; i++) { … }");

Through this, you can easily 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 strength of the original Dynamic Language. Now C # can be done as well!

If you are interested, you can find this talk below: http://channel9.msdn.com/pdc2008/tl16 /. Both online and download are available.

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

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.