In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 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 C#forced type conversion has several kinds of content. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
C#forced type conversion has the following three types:
Note: objA is typeA, objB is typeB
(1) typeA objA = (typeA)objB;
(2) typeA objA = objB as objA;
(3) typeA objA = objB as typeA
In the actual development process we often use the above three types of type conversion, but the difference between the three C#mandatory type conversion, and how to choose in use?
When we use *** type conversion, if typeA is an upper class of typeB, the type conversion can be performed correctly. If typeB is an upper class of typeA, System.InvalidCastException will be thrown. So in real development, to avoid abnormal releases, we might use the following code:
If(objB is typeA) { objA = (typeA)objB; }
Among them, is the second type conversion. According to the explanation on MSDN, its role is to "check whether the object is compatible with the given type". Although it only returns a boolean value, it also judges whether it is compatible by type conversion. Therefore, when using this statement, a type conversion will be performed. Then, if typeB is compatible with typeA, the above code will perform the type conversion of (typeA)objB again, which will definitely affect the performance of the program we developed. Is there a better way to help us solve this problem?
Let's look at the third C#mandatory type conversion.
In the third type conversion, if an instance of type B objB cannot be converted to type A, a null value will be returned, and if the conversion is successful, an instance of type A will be returned. This conversion does not cause exceptions and requires only one type conversion. This solves the performance cost of the above two type conversions.
Thank you for reading! About "C#forced type conversion has several kinds" This article is shared here, I hope the above content can have some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!
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.