C-Sharp Console Type Conversion Function Examples

C-Sharp Console Type Conversion Function Examples

Purpose: – Illustrates for .


C-Sharp Console Type Conversion Function Examples

Type Conversion Functions Input Expression Result
Convert.ToBoolean(expression) Char, Numeric, or String Boolean
Data
Type
Convert.ToByte(expression) 0 to 255 unsigned (gets rounded) Byte
Data
Type
Convert.ToChar(expression) Char or String (if string, only first character is used) Char
Data
Type
Convert.ToDate(expression) Date and/or time in any valid format. Date
Data
Type
Convert.ToDouble(expression) -1.79769313486231570E+308 through -4.94065645841246544E-324 for negative values;4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values. Double
Data
Type
Convert.ToDecimal(expression) +/-79,228,162,514,264,337,593,543,950,335 for zero-scaled numbers, that is, numbers with no decimal places. For numbers with 28 decimal places, the range is +/-7.9228162514264337593543950335. The smallest possible non-zero number is 0.0000000000000000000000000001 (+/-1E-28). Decimal
Data
Type
Convert.ToInt16(expression) -2,147,483,648 through 2,147,483,647; fractional parts are rounded . Integer
Data
Type
Convert.ToInt64(expression) -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807; fractional parts are rounded. Long
Data
Type
(object)(expression) Any valid data type. Object
Data
Type
Convert.ToSByte(expression) -128 through 127; fractional parts are rounded. SByte
Data
Type
Convert.ToInt32(expression) -32,768 through 32,767; fractional parts are rounded Short
Data
Type
Convert.ToSingle(expression) -3.402823E+38 through -1.401298E-45 for negative values; 1.401298E-45 through 3.402823E+38 for positive values Single
Data
Type
Convert.ToString(expression) 1. If boolean, returns string “True” or “False” 2. If date, returns A string containing a date value (date and time) in the short date format of your system 3. If number, returns a string representing the number String
Data
Type
Convert.ToUInt16(expression) 0 to 4,294,967,295 – will throw exception if negative number is passed in. Rounds fractions to whole numbers UInteger
Data
Type
Convert.ToUInt64(expression) 0 to 18,446,744,073,709,551,615 (unsigned) – will throw Exception if negative number is passed in. Rounds fractions to whole numbers ULong
Data
Type
Convert.ToUInt32(expression) 0 to 65,535 (unsigned) – will throw Exception if negative number is passed in. Rounds fractions to whole numbers SByte
UShort
Type