Convert data types in C #
Converting data types in C # is to convert one data type to another. It is also known as Type Injection. In C #, type casting has the following two patterns:
Implicit conversion (implicit) - This conversion is done by C # in a type-safe method. For example, conversion from inheriting classes to base classes.
Explicit conversion (explicit) - This conversion is explicitly performed by the user using predefined functions. Explicit type conversions need a cast operator.
The following example illustrates an explicit transformation in C #:
using System ; namespace QTMCsharp { class TestCsharp { static void Main ( string [] args ) { Console . WriteLine ( "Chuyen doi kieu du lieu trong C#" ); Console . WriteLine ( "-------------------------------" ); double d = 5678.74 ; int i ; // cast kieu du lieu double thanh kieu du lieu int. i = ( int ) d ; Console . WriteLine ( "Gia tri cua i = " + i ); Console . ReadKey (); } } }
Compiling and running the above C # program will produce the following results:
Method to convert data types in C #
C # provides the available type conversion methods listed in the following table:
Convert a type to a Boolean value, if possible
ToByteConvert a type to a byte
ToCharConvert a style to a Unicode character, if possible
ToDateTimeConvert a type (integer or string) into date-time structures
ToDecimalConvert a real number or integer to a decimal type
ToDoubleConvert a type to a double type
Tont16Convert a style into a 16-bit integer
ToInt32Convert a style into a 32-bit integer
ToInt64Convert a style into a 64-bit integer
ToSbyteConvert a type to a signed byte type
ToSingleConvert a style to a number of small floating points
ToStringConvert a style to a string
ToTypeConvert a type to a specified type
ToUInt16Convert a type to an unsigned int type
ToUInt32Convert a type to an unsigned long type
ToUInt64Convert a type into an unsigned big integer
The following example illustrates how to convert diverse data types into string data types in C #:
using System ; namespace QTMCsharp { class TestCsharp { static void Main ( string [] args ) { Console . WriteLine ( "Chuyen doi kieu du lieu trong C#" ); Console . WriteLine ( "-------------------------------" ); int i = 75 ; float f = 53.005f ; double d = 2345.7652 ; bool b = true ; //su dung phuong thuc ToString() Console . WriteLine ( i . ToString ()); Console . WriteLine ( f . ToString ()); Console . WriteLine ( d . ToString ()); Console . WriteLine ( b . ToString ()); Console . ReadKey (); } } }
Compiling and running the above C # program will produce the following results:
Follow tutorialspoint
Previous article: Data type in C #
Next post: Array (Array) in C #
You should read it
- CAST function in SQL Server
- Data type in C #
- Data type in C / C ++
- Data types in SQL Server
- Data type in C programming
- Chromebooks can now warn users when connecting to an 'inappropriate' USB Type C cable
- What is USB Type C? Type C cable models are widely used
- TYPE function - Function that returns the data type of any value in Excel
May be interested
- Array (Array) in C #an array stores a set of fixed-size elements in the same type. an array is used to store a data set, but it is often more useful to think of an array as a set of variables of the same type stored in adjacent memory locations.
- String (String) in C #in c #, you can use strings (strings) as array of characters. however, it is more common to use string keywords to declare a string variable. the string keyword is an alias for the system.string class in c #.
- Structure (Struct) in C #in c #, a structure is a data type. it helps you create a single variable that keeps relevant data of diverse data types. the keyword struct in c # is used to create a structure.
- Class (Class) in C #when you define a class in c #, you define a blueprint for a data type. this does not really define any data, but it defines the meaning of that class name. that is, what an object of that class consists of, what activities can be performed on that object.