In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "Why user-defined conversions must be converted to closed types or from closed types". In daily operations, I believe that many people have doubts about why user-defined conversions must be converted to closed types or from closed types. Xiaobian consulted all kinds of materials and sorted out simple and useful methods of operation. I hope it will be helpful to answer the question of why user-defined conversions must be converted to or from closed types! Next, please follow the editor to study!
Encounter this error message when doing implicit type conversions: "user-defined conversions must be converted to closed types or from closed types":
The statement is roughly as follows:
There are two classes: an and B, in which class An and class B have exactly the same structure, except that class B belongs to a third party.
You need to return a List in a method of class C, such as:
Public List Func () {List list = new List;. Return list;}
At this time, an error will be reported due to different return types.
Implicit conversion in class A
Class A {.... Public static implicit operator List (List value) {.}.}
At this time, the error "user-defined conversions must be converted to or from closed types" will be reported.
Check out MSDN, and there are two sentences in the type conversion section that say:
1. Operands must be of closed type
two。 The type conversion definition from class A to class B cannot be done in class C (that is, the conversion of two classes cannot be defined in the third class).
At first, it was believed that the compilation error was caused by the first point, and it was also mistakenly believed that the so-called closed type must be a class / structure, not a collection object, but it was later found that the second reason for this error was the second point. because when I change the definition of type conversion to:
Class A {.... Public static implicit operator A (List value) {.}.}
When the compilation is passed, the first point is excluded.
It turns out that for the compiler, class An is a type, List is also a type, and List is also a type, so the implicit conversion of List types to List types cannot be done in class A, which conforms to the second point.
Add:
If you want to be in a project, there are ways to:
Public List Func () {List list = new List;. Return list;}
How do I convert this list to List?
In fact, the method is also very simple. Since the implicit conversion of List cannot be done, you can convert the elements in list one by one when you return:
Implicit conversion definition:
Class A {.... Public static implicit operator A (B value) {.}.}
Method definition:
Public List Func () {List list = new List;. Return list.Select (p = > (B) p). ToList ();} at this point, the study of why user-defined conversions must be converted to or from closed types is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.