In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the conversion method of data type in C# language". In daily operation, I believe that many people have doubts about what the conversion method of data type in C# language is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the question of "what is the conversion method of data type in C# language"! Next, please follow the editor to study!
I. Type conversion
In the next day's study, I learned the basic data types in the C # language, such as integer type int, floating point type float, Boolean type bool, character type char, and so on. There are some questions at this time. If integers and floating-point numbers are added, does the result return an integer or a floating-point type? Can values of numeric types be arithmetically manipulated with character types? What is the result of Boolean values and other types of operations?
To answer these questions, you need to know something about data type conversion.
1. Change the form
There are also two forms of C# data types:
(1) implicit type conversion-this is a C # default conversion in a secure form. For example: from a small number type to a large integer type, from a derived class to a base class.
Int I = 3terlong l = I; / l = 3
(2) Show type conversions-that is, cast operators are required. Using explicit conversions may result in missing data.
Int I = 1000000 × short s = (short) I; / / s = 169602, different types of conversion
Next, let's take a look at which of the different data types need to show conversions and which are implicit conversions.
(1) floating point numbers and integer types
Int a = 3 5float float b = 2.3f leading int c = a + (float) b; / c = 5float d = a + b; / / d = 5.3f
Integer types can be implicitly converted to floating-point numbers by default, while floating-point numbers need to use strong transfer operators of integer types if they are to be converted to integers.
After a floating-point number is strongly converted to an integer type, only the integer part is retained, and the decimal part is lost.
(2) character and integer types
Char a = 'Apostatic int x = c; / x = 65int b = 49 politic char y = (char) b; / / y =' 1'
The value of a character type can be implicitly converted to an integer type, while an integer type requires a char override if it is to be converted to a character type.
The reason why characters can be converted to integer types is that in coding tables such as ASCII or Unicode, each character corresponds to a number. To a computer, however, these numbers are a string of binaries. Although there is a big difference between numbers and words in our view, they are only binary numbers for computers.
(3) Boolean type
Bool ntint a = 1 float n = (bool) a; / / error float b = 2.3f n = (bool) b; / / error char c = '0birthday n = (bool) c; / / error
Boolean types cannot be implicitly converted directly to other types, nor can they be cast directly using the type strong transition operator.
If you want to convert to a Boolean type, you need to introduce some data type conversion methods built into C#.
3. Method of type conversion
(1) Convert.ToBoolean (param)
Bool b * string str = 'false';b = Convert.ToBoolean (str); / b = Falseint num = 1 * * b = Convert.ToBoolean (num); / b = Truefloat ff = 3.24 f * * b = Convert.ToBoolean (ff); / / b = True
(2) Convert.ToInt32 (param)
Int I = 49string num (word); I = Convert.ToInt32 (num); / / I = 321bool boo = true;i = Convert.ToInt32 (num); / / I = 1
The methods for converting integer types are: ToInt16 is equivalent to short type; ToInt32 is equivalent to Int type and is the most commonly used in programs; ToInt64 is equivalent to Long type.
This method is usually applied to convert to an integer type when a user input value is received.
(3) ToString ()
String str;int num = 3234 / num.ToString (); / / str = "3234" bool boo = false;str = boo.ToString (); / / str = "false" float foo = 32.43f / foo.ToString = foo.ToString (); / / str = "32.43"
The functions of the Convert.ToString (param) and ToString () methods are similar. The latter is an extension method of Object, so all types have the method ToString (). The parameters in the former can make various types of underlying data types, or Object class objects.
In general, both of the above methods can be used. However, when the null value may appear in the returned data type, the ToString method is called, and NullReferenceException is returned. Instead of throwing an exception, Convert.ToString returns an empty string.
(4) other conversion methods
Convert.ToUint32-converts to an unsigned integer type. Very similar to Convert.ToInt32, both have 16-bit and 64-bit.
Convert.ToDecimal-converts a floating point number or integer to a Decimal decimal type.
Convert.Double-converts to the Double double-precision floating-point type. ...
At this point, the study on "what is the conversion method of data types in C# language" is over. I hope to be able 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.