In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the conversion method between C# numerical types". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Conversion between C # numeric types
The numerical types mentioned here include byte, short, int, long, fload, double and so on. According to this order, various types of values can be automatically converted backward. For example, if you assign a data of type short to a variable of type int, the short value is automatically converted to a value of type int and then assigned to a variable of type int. Such as the following example:
Private void TestBasic () {byte a = 1; short b = a; int c = b; long d = c; float e = d; double f = e; this.textBox1.Text = ""; this.textBox1.AppendText ("byte a =" + a.ToString () + "\ n"); this.textBox1.AppendText ("short b =" + b.ToString () + "\ n"); this.textBox1.AppendText ("int c =" + c.ToString () + "\ n") This.textBox1.AppendText ("long d =" + d.ToString () + "\ n"); this.textBox1.AppendText ("float e =" + e.ToString () + "\ n"); this.textBox1.AppendText ("double f =" + f.ToString () + "\ n");}
The translation passed smoothly, and the result is that the value of each variable is 1; of course, their types are still System.Byte type. System.Double type. Now let's try it. What if we reverse the order of assignment? Append the following statement to the TestBasic () function:
Int g = 1; short h = g; this.textBox1.AppendText ("h =" + h.ToString () + "\ n")
As a result, the compilation reported an error:
G:\ Projects\ Visual C #\ Convert\ Form1.cs (118): the type "int" cannot be implicitly converted to "short" where the 118th line of Form1.cs is the line of short h = g.
At this time, if we insist on conversion, we should use cast, which is often mentioned in the C language, that is, to use a statement in the form of "(type name) variable name" to cast data. As in the above example, the modification is as follows:
Short g = 1; byte h = (byte) g; / / the value of g of short type is forcibly converted to short type and then assigned to the variable h this.textBox1.AppendText ("h =" + h.ToString () + "\ n")
The compilation passed, and the output of the running result was h = 1, and the conversion was successful.
However, if we use cast, we have to consider another question: the range of short type is-32768-23767, while the range of byte type is 0-255. what happens if the size of the variable g exceeds the range of byte type? We might as well rewrite the code again to change the value to 265, which is 10. 5% larger than 255.
Short g = 265; / / 265 = 255 + 10 byte h = (byte) g; this.textBox1.AppendText ("h =" + h.ToString () + "\ n")
There is no error in the compilation, but the result is not h = 265, but h = 9.
Therefore, when we carry out the conversion, we should pay attention to that the converted data cannot go beyond the scope of the target type. This is not only reflected in the conversion of multi-byte data types (relative, such as short in the above example) to few-byte types (relative, such as byte in the above example), but also between signed and unsigned types with the same number of bytes, such as converting 129s of byte to sbyte will overflow. The examples in this respect are more or less the same, so they will not be explained in detail.
This is the end of the content of "what is the method of conversion between C# numerical types". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.