In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what the type conversion operator is in .NET. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
1. Introduction
Type safety is one of the key considerations at the beginning of. Net design. For programmers, it is often inadequate to fully grasp the type safety of system data. Now, all this has been solved for you in the design framework of Microsoft Daniel. In. Net, all types must be integrated from the System.Object type, so we can easily get the exact type of the object by: the GetType () method. So what should be considered about type conversion in .NET?
two。 Concept introduction
Type conversions include display conversions and implicit conversions. The basic rules for type conversions in .NET are as follows:
Any type can be safely converted to its base class type, which can be done by implicit conversion
When any type is converted to its derived type, the display conversion must be performed, and the rule of conversion is: (type name) object name
You can get the exact type of any object using GetType
Basic types can be converted using the Covert class
All types except string have Parse methods that convert the string type to the corresponding base type
The conversion mechanisms for value types and reference types are called boxing and unboxing.
3. Principle and example explanation
After talking about several common aspects of type conversion, it's time to focus on the vendetta of the is and as operators. Type conversion will be a big topic, leaving it for discussion at an appropriate time.
The is/as operator, which is used for type conversion in C #, provides the judgment of type compatibility, which makes the type conversion control in the safe category and provides flexible type conversion control.
The rules for is are as follows:
Check the compatibility of object types and return the result, true or false
If the object is null, the return value is always false.
The copy code is as follows:
Namespace Learning
{
Public partial class check: System.Web.UI.Page
{
Object o = new object ()
Protected void Page_Load (object sender, EventArgs e)
{
Response.Write (o.GetType ()); / / System.Object
If (o is System.Object) / / performs the first type compatibility check, and o is A returns false
{
Response.Write ("o is System.Object")
}
}
}
}
The rules for as are as follows:
Check the compatibility of object types and return the result. Return null if it is not compatible.
If the result is judged to be empty, enforcing the cast throws a NullReferenceException exception.
The copy code is as follows:
Namespace Learning
{
Public partial class check: System.Web.UI.Page
{
Object o = new object ()
Protected void Page_Load (object sender, EventArgs e)
{
Response.Write (o.GetType ()); / / System.Object
If ((o as System.Object)! = null) / / performs the first type compatibility check, and o is A returns false
{
Response.Write ("o is System.Object")
}
}
}
}
This is the end of the article on "what is the type conversion operator in .NET". 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, please share it 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.