In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about what you need to pay attention to in C# type conversion. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Neither the as nor is operators perform any user-defined transformations.
2. For a cast, a null reference will cause a conversion error.
3. There is a difference in the presentation of IL code between any type of cast and a custom conversion.
4. User-defined conversions only act on the compile-time type of the object.
5. The as operator cannot be applied to value types.
6. Mandatory type transformation is used in foreach loop statements.
Let's introduce them one by one:
1. Let's first look at the error code example:
ClassA {} classC {publicstaticimplicitoperatorA (Ct) {returnnewA ();}} classProgram {staticvoidMain (string [] args) {objecto=Factory.GetObject (); / o is a C type: Aa=oasA;// transformation failed, o is not A}}
The code has been very clear, we can not define the C to A conversion, we can not use as, corresponding to user-defined conversion, we can only use (A) o conversion, in fact, custom conversion and custom operation =, +, -, ",% and other operators are the same mechanism, so you can understand why you can not use custom conversion with as.
2, or look at an error example:
ClassA {} classB:A {} classProgram {staticvoidMain (string [] args) {Bb; Aa= (A) b;}} problem needless to say, as can solve this problem. 3, look at an example: classA {} classC {publicstaticimplicitoperatorA (Ct) {returnnewA ();}} classB:A {} classProgram {staticvoidMain (string [] args) {Aa=newB (); Bb= (B) a; Cc=newC (); a = (A) c;}} for Bb= (B) a; the IL code for IL_0008:castclassConsoleApplication1.B is as follows: IL_0008:castclassConsoleApplication1.B for a = (A) c The IL code is as follows: IL_0015:callclassConsoleApplication1.AConsoleApplication1.C::op_Implicit (classConsoleApplication1.C) the distinction you have seen, to really understand the difference, then we need to continue to talk about "4, user-defined conversions only act on the compile-time type of the object." 4, look at an example: classA {} classC {publicstaticimplicitoperatorA (Ct) {returnnewA ();}} classB:A {} classProgram {staticvoidMain (string [] args) {Aa=newB (); Bb= (B) a; objectc=newC (); a = (A) Cramp / compile passed, failed to run! }}
Maybe you never thought a = (A) c; the compilation will be successful, and the run will make an error in this sentence. After all, we have a conversion from type C to type A, and we can find the answer by looking at the IL code.
.methodprivatehidebysigstaticvoidMain (string [] args) cilmanaged {.entrypoint / / Codesize28 (0x1c) .maxstack1.localsinit ([0] classConsoleApplication1.Aa, [1] classConsoleApplication1.Bb [2] objectc) IL_0000:nop IL_0001:newobjinstancevoidConsoleApplication1.B::.ctor () IL_0006:stloc.0 IL_0007:ldloc.0 IL_0008:castclassConsoleApplication1.B IL_000d:stloc.1 IL_000e:newobjinstancevoidConsoleApplication1.C::.ctor () IL_0013:stloc.2 IL_0014:ldloc.2 IL_0015:castclassConsoleApplication1.An IL_001a:stloc.0 IL_001b:ret} / / endofmethodProgram::Main
Take a look at the sentence IL_0015:castclassConsoleApplication1.A, which indicates that the custom conversion takes place at compile time. You may want to ask why it is not in Article 3:
IL_0015:callclassConsoleApplication1.AConsoleApplication1.C::op_Implicit (classConsoleApplication1.C)
Because objectc,c is defined as an Object type, the cast goes to Object at compile time to see if there is a custom conversion operation (note that the compiler gives priority to custom conversion during the transition and does not castclass until it is not found). Of course, Object does not have a custom conversion to An operation, then use the normal cast castclass. Well, now we know that user-defined conversions only work on the compile-time types of objects, while normal Bb= (B) a; casts can work at run time. So how to get rid of the above mistakes? The corresponding code is modified as follows:
Objectc=newC (); Ccc=casC; a = (A) cc
Now that it's running normally, let's look at the IL code again.
IL_001c:callclassConsoleApplication1.AConsoleApplication1.C::op_Implicit (classConsoleApplication1.C)
By now, everyone's understanding of 3 and 4 should be very clear.
5. The as operator cannot be applied to value types-omit! In view of the simplicity of this point, I will not provide an example. If you are interested, you can try it on your own.
6. Look at the code example: (the following code is excerpted from "50 ways to improve the C# Program in EffectiveC# Chinese version"-page 23)
PublicvoidUseCollection (IEnumerabletheCollection) {foreach (MyTypetintheCollection) t.DoStuff ();} / / the above code is equivalent to: publicvoidUseCollection (IEnumerabletheCollection) {IEnumeratorit=hteCollection.GetEnumerator (); while (it.MoveNext ()) {MyTypet= (MyType) it.Current; t.DoStuff ();}}
By looking at the IL code, we can confirm that the conversion of the foreach statement is a cast operation used, so why? The reason for using forced transformation is that the foreach statement needs to support both value types and reference types, which shows that our fifth point, as, does not support value types.
Thank you for reading! This is the end of this article on "what do you need to pay attention to in C# type conversion"? I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it out for more people to see!
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.