C # Note 1-change resistance and coordination,

Source: Internet
Author: User

C # Note 1-change resistance and coordination,

Preface

I have been working for a year. I also like reading books and walking around the garden. But when talking about writing blogs, I really don't have to. In the end, there is only one word: lazy! Now I want to get rid of this "problem". I hope to record the knowledge I learned from my daily work and the problems I encountered. First, I can sort out my ideas and deepen my understanding; second, you can learn from and share with more friends. Third, you can exercise your own writing skills. It's a benefit!

At ordinary times, I may encounter some minor problems, such as checking and remembering, or simply taking notes. At that time, I understood the problem and did not form a document. After a while, I encountered the same problem, it is very troublesome to try again. I hope to write these things into articles in the future, even though it may be a small problem, as well as taking notes. Some concepts, syntaxes, or norms of c # are recorded in 【C # Notes]. Because it is not for beginners to learn and remember, there is no certain time and learning sequence, but it is recorded if it is necessary at ordinary times.

I. Problems Encountered

The job is developed based on. net3.5 and encountered a problem in the actual process. Suppose we have a Base class, a Derived class, and Derived inherits Base. As follows:

    class Base    {    }    class Derived : Base    {    }  

When I use IEnumerable <Base> as the form parameter and List <Derived> as the real parameter, I find a compilation error! The original parent class is used as the form parameter, but it is not normal to pass the subclass, but the compilation fails in the generic type.

Ii. Exploring Problems

  We usually have a principle in designing parameters and return values,The parameter should be as "generic" as possible, and the return value should be as "fine" as possible ". Generic refers to the use of interfaces or parent classes as parameters, so that more parameter types can be received; fine, refers to the return of specific types, which can better describe the role of the method.

For example:

String [] strs = new string [] {"hello", "word"}; // The disadvantage is that the array cannot be passed and a ToList () call is required () static void Test_1 (List <string> list) {}// the correct method should be: IEnumerable <T> static void Test_2 (IEnumerable <string> list ){}

It can be seen that the "wildcard" of parameters can provide greater flexibility.

Next, we will enter the subject: resistance to changes and coordination changes. It should be noted that the resistance to changes and the covariant are supported at the beginning of 4.0. Assume that there is a method that requires the Derived set as the parameter. Based on the above principle, we will design it as follows:

  static void TestIn(IEnumerable<Base> bases)  {  }

Next we will call the following method. In 3.5, we will find that the compilation fails, prompting that the List <Derived> cannot be converted to IEnumerable <Base>.

 List<Derived> listIn = new List<Derived>(); TestIn(listIn);

For the same code, we got 4.0 and found that the compilation was successful. Comparing IEnumerable generic interfaces, we found that the definition in 4.0 is:

 public interface IEnumerable<out T> : IEnumerable

If the out keyword is found to be missing, This is the covariant.Msdn explains the type parameter as follows: the type of the object to be enumerated by out T. This type of parameter is covariant. That is, you can use the specified type or a type with a higher degree of derivation.

  In this way, we can understand the covariant. The parameter type is the covariant, and the parent class is replaced by the subclass, that is, the subclass is used as the parent class.

After understanding the covariant, it is easy to understand it.The Return Value of the function is variable-resistant. The Child class is replaced by the parent class, that is, the Child class is used as the Child class. In the case of non-generic type, we can receive the return value of the method as follows:

  object obj = Test_3();  static string Test_3()  {    return "hello world";  }  

  Of course, we think this call should also be possible:

  IEnumerable<Base> listOut = TestOut();  static IEnumerable<Derived> TestOut()  {    return new List<Derived>();  }

In 3.5, this will also cause compilation errors. In 4.0, there is no problem.

Iii. Summary

In fact, we often encounter the following concepts (Parameter covariant and return value Resistance), And we will also get used to this design. However, for generics,. net only provides such support by 4.0, which provides more flexibility for the use of generics.

Okay. Actually, we don't need to understand conceptual things very much. Just know the principle and how to use it. The above is my personal understanding. For more in-depth understanding, see msdn.

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.