Generic in C #2.0

Source: Internet
Author: User

Today, a Microsoft developer gave us three lectures, including the new features in C #2.0. I am familiar with generic programming in C ++, so I came up with several questions when I spoke about it.

In C #1.0, the default value of the method parameter is not supported, which can only be simulated through overload. Similarly, in the generic class of C #2.0, it is believed that the default value of template parameters will not be provided. In C ++, It is very common to provide the default template parameter values, and it can help the client call generic class more easily, such as set in C ++ STL:

Template < Class _ Key, Class _ Compare = Less < _ Key > ,
Class _ Alloc = Allocator < _ Key >   >
Class   Set ;

 

To provide such convenience in C #2.0, you must support two features: the default value of the template parameter and the reference of the previous template parameter. Unlike the default values of method parameters, it is difficult to simulate the default values of template parameters, especially those based on previous parameters.

In addition to the default parameters, it is not clear whether the non-type template parameters are supported in C #2.0. Currently, this parameter is not supported, it is not easy to add this function according to the existing syntax.

The where keyword is provided in C #2.0 for the restriction of generic parameters. For example, the restriction must implement specific interfaces.

Full specialization and partial specialization in C ++ do not seem to be supported either in C #2.0. This is for the traits formed in C ++, meta-programming and other ideas cannot be well applied in C #2.0. But what makes a big difference with C ++ is that all methods in C # must be defined in the class. Therefore, specialization also has a substitute solution for methods, that is, overload.

For exampleCode:

Public   Class Generictest < T >
{
Public   Static Void Foo (T Arg)
{
Console. writeline ("One");
}

Public   Static Void Foo ( Int Arg)
{
Console. writeline ("Two");
}
} ;

Public   Class Normaltest
{
Public   Static Void foo < T > (T Arg)
{
Console. writeline ("One");
}

Public   Static Void Foo ( Int Arg)
{
Console. writeline ("Two");
}
} ;

If you call normaltest. foo (1), the output will be "two", that is to say, C #2.0 preferentially selects the more specialized overload, only after all specialized reloads do not match, generic version. Note that the overload resolve does not consider the modifier protection level. the public of Foo (INT) is changed to private, and normaltest is called. when the code of Foo (1) is compiled, an error occurs. The Compiler considers that a private method is called from outside the class.

In the case of generictest, if Foo (1) is called from a static method, I think the method Foo (INT) is called, however, if generictest <int>. foo (1) method call, I still do not know whether to output "one", "two" or compile error. Because my current machine has not installed. NET 2.0 Beta, it cannot be verified. If you have installed. NET 2.0 Beta, try it. Also, if you call normaltest. Foo <int> (1), you also need to verify the output. Just speculation is unreliable.

Because the partial type is provided in C #2.0, it is effective to provide the specialized version of a generic method in the class in multiple files. In general, although C #2.0 implements only a limited number of generic functions, it is also a complete set of functions, which is very convenient to use.

A long time ago, I saw some introductions of C #2.0, but I haven't paid much attention to them for a long time. I only think of these things in today's lecture. You are welcome to discuss them with me.

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.