当前位置:网站首页>Type conversion in C #
Type conversion in C #
2022-04-23 06:25:00 【Wood acridine】
C# Type conversion in
- The concept of type conversion
C# Is a strong type of language , Strict requirements for types , But under certain conditions, they can also be converted to each other , Such as the int Type data into double Type data .
C# Two conversion methods are allowed : Implicit type conversion and display type conversion .
- Implicit type conversion
Implicit type conversion is C# Default safe conversion , No data loss . for example , Convert from small integer type to large integer type . From a derived class to a base class .
Implicit conversion is mainly in integer 、 Conversion between floating point types , Change the data type with small storage range directly to the data type with large storage range .
Implicit numerical conversion includes the following :
- from sbyte Type to short,int,long,float,double or decimal type .
- from byte Type to short,ushort,int,uint,long,ulong,float,double or decimal type
- from short Type to int,long,float,double or decimal type
- from ushor Type to int、uint、long、ulong、float、double or decimal type .
- from int Type to long、ffloat、double or decimal type .
- from uint Type to long、ulong、float、double or decimal type .
- from long Type to float、double、 or decimal type .
- from ulong Type to float、double or decimal type
- from char Type to ushort、int,uint、ong、ulong、float、double or decimal type
- from float Type to double type .
Implicit value conversion is actually the conversion from low-precision value type to high-precision value type .
2. Display type conversion
Display type conversion , Forced type conversion . Conversion requires cast operator , And the cast will cause data loss .
Forced type conversion is mainly used to convert data types with a large storage range to data types under the storage range , But data types need to be compatible .
The syntax of cast is as follows :
Data type variable name =( data type ) Variable name or value ;

- Method of data type conversion
- Parse Method
C# Parse Method is used to character string Type conversion to Any basic type , The specific grammatical form is as follows .
Basic data type .Parse( Value of string type );
The data types on the left and right sides of the equal sign are required to be compatible .
Examples are as follows :

- Convert Method
Convert Method is the most flexible method in data type conversion , It can convert the value of any data type to any data type , The premise is not to exceed the range of the specified data type .
The specific grammatical form is as follows :
data type Variable name =convert . To data type ( Variable name );
here Convert . To The following data type should match the data type to the left of the equal sign .
Convert The common type conversion methods of class are shown in the following table :
| Method |
explain |
| Convert.ToInt16() |
Convert to plastic (short) |
| Convert.ToInt32() |
Convert to plastic (int) |
| Convert.ToInt64() |
Convert to plastic (long) |
| Convert.ToChar() |
Convert to character (char) |
| Convert.ToString() |
Convert to string (string) |
| Convert.ToDateTime() |
Convert to date type (datetime) |
| Convert.toDouble() |
Convert to double precision floating-point number (double) |
| Convert.ToSinge() |
Convert to single precision floating point (float) |
Examples are as follows :

- Unpacking and packing
Packing : Convert value type to reference type .
Sealing box : Converts a reference type to a value type .
Use the function of packing and unpacking , You can use any value of the allowed value type with object The values of type are converted to each other , Connect value types with reference types .


版权声明
本文为[Wood acridine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210616405145.html
边栏推荐
- [leetcode169] most elements
- Collections multiple parameter sorting
- 程序設計訓練
- MySQL best practices for creating tables
- [leetcode 6] zigzag transformation
- Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
- The bottom implementation principle of thread - static agent mode
- The problem that the page will refresh automatically after clicking the submit button on the form is solved
- How to grow at work
- Linear algebra Chapter 1 - determinant
猜你喜欢
随机推荐
What is the difference between the basic feasible solution and the basic feasible solution in linear programming?
JDBC connection database
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
Implementation of displaying database pictures to browser tables based on thymeleaf
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
Create binary tree
Guaba and Computational Geometry
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
Addition, deletion, modification and query of MySQL table
[leetcode 228] summary interval
线性代数第三章-矩阵的初等变换与线性方程组
Linear algebra Chapter 1 - determinant
Calculation (enter the calculation formula to get the result)
Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
Rainbow (DP)
线代第四章-向量组的线性相关
10.Advance Next Round
Optional best practices
How does MySQL convert stored seconds into dates


![[leetcode 54] spiral matrix](/img/c0/9a55a62befb783a5bfc39dc3a96cb2.png)




![[leetcode 59] spiral matrix II](/img/6e/58e600272797563129d2b325a054b5.png)

