Generic: a better matching method]

Source: Internet
Author: User

Original address: http://www.srtsolutions.com/generics-the-better-method-match? Utm_source = feedburner & utm_medium = feed & utm_campaign = Feed % 3A + billwagner + % 28 Bill + Blogs + in + C % 23% 29

 

This problem occurs again in a C # developer email list I shared. I think I will release the rules and the reasons behind these rules because of the chaotic number of such topics.

 

All the things published here come from <C # Language Specification> (Fourth Edition) Section 7.5.2 (type reference) and section 7.5.3 (reload resolution ).

 

Consider the overload methods of these two logs:

static void Log<K,V>(K key, V value)
static void Log<K,V>(string key, IDictionary<K, V> values)

Assume that you call the following code:

 

class FooDict : Dictionary<string, int>
{
}
// create custom dictionary
FooDict fooDict = new FooDict();
// should *not* use the IDictionary<K,V>
Log("FooDict", fooDict);
// try a regular dictionary value
Dictionary<string, int> dict = fooDict;
// should *not* use the IDictionary<K,V> 
Log("Dictionary", dict);
// now try the EXACT type
IDictionary<string, int> idict = fooDict;
// should use the IDictionary<K,V> - does
Log("IDictionary", idict);
 
The comment describes the Code call execution. Why is this result?
 
Before executing any operation, the compiler will first look for all the appropriate candidate methods, which may include generic methods. If the method call does not specify the specific parameter type, the compiler will deduce the parameter type. Eric Lippert has explained in detail (below)
The type inference rule is designed to answer a question: only real parameters and formal parameter types are given, and what is the most appropriate real parameter is inferred to each type parameter.
For the first overload method, Log <string, FooDict> is the most matched parameter type. Now there are two candidate methods for analyzing the overloaded operation rules. Using type inference, the first overload method is more matched, because the first overload method is called to determine the conversion: Log <string, FooDict> is a definite conversion from Log <string, FooDict>, calling the second overload method is an inheritance conversion. The Log <string, FooDict> is inherited from Log <string, IDictionary <string, int>, because the FooDict class inherits the IDictionary <string, int> interface. Determine that the conversion takes precedence over the Interface Conversion.
The same processing logic can be applied to other inheritance transformations, such as converting to the parent class, length conversion (for example, converting the int type to the long type), or user-defined class inheritance conversion.
 
The reference specification is the same as "that works like this", but why is this the right option for programming languages? To put it simply, because the type conversion takes effect when the interface type is actually compiled (note: the generic type has taken effect after being generated ). The completed part has been released to C # Puzzlers Live Lessons.
 
 
Complete test run code written by myself:
class Program
    {
        static void Main(string[] args)
        {
            // create custom dictionary
            FooDict fooDict = new FooDict();
            // should *not* use the IDictionary<K,V>
            Logger.Log("FooDict", fooDict);
            // try a regular dictionary value
            Dictionary<string, int> dict = fooDict;
            // should *not* use the IDictionary<K,V> 
            Logger.Log("Dictionary", dict);
            // now try the EXACT type
            IDictionary<string, int> idict = fooDict;
            // should use the IDictionary<K,V> - does
            Logger.Log("IDictionary", idict);
        }
    }
     class FooDict : Dictionary<string, int>
    {
    }
    class Logger
    { 
        public static void Log<K, V>(string key, IDictionary<K, V> values)
        {
            Console.WriteLine("call:Log<K, V>(string key, IDictionary<K, V> values)");
        }
        public static void Log<K, V>(K key, V value)
        {
            Console.WriteLine("call:Log<K,V>(K key, V value)");
        }
    }

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.