当前位置:网站首页>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
边栏推荐
- Create binary tree
- [leetcode217] there are duplicate elements
- D. Optimal partition segment tree optimization DP
- 电机与拖动(戚金清版)学习整理
- Example of ticket selling with reentrant lock
- [leetcode169] most elements
- Pytorch notes - get familiar with the network construction method by building RESNET (complete code)
- Calculation (enter the calculation formula to get the result)
- Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)
- Linear algebra Chapter 2 - matrices and their operations
猜你喜欢
Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
Addition, deletion, modification and query of MySQL table
C language file operation
Illustrate the significance of hashcode
Best practices for MySQL storage time
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
JDBC connection database
Fundamentals of digital image processing (Gonzalez) I
Practical operation - Nacos installation and configuration
自動控制(韓敏版)
随机推荐
Kalman filter and inertial integrated navigation
Pytorch introduction notes - use a simple example to observe the output size of each layer of forward propagation
線性代數第一章-行列式
[leetcode169] most elements
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
Customized communication between threads (reentrantlock)
Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention
2. Average length of words
3. Continuous integer
@Problems caused by internal dead loop of postconstruct method
The problem that the page will refresh automatically after clicking the submit button on the form is solved
图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi
SQL injection
Practical operation - Nacos installation and configuration
4. Print form
Pyqt5 learning (I): Layout Management + signal and slot association + menu bar and toolbar + packaging resource package
IO multiplexing of 09 redis
Protected (members modified by protected are visible to this package and its subclasses)
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
Graphic numpy array matrix