The following table summarizes the C# data types.
Type | Size | Value |
bool | 2 bytes | Must be true or false . |
byte | 1 byte | 0 to 255 (unsigned byte). |
sbyte | 1 byte | –128 to 127 (signed byte). |
char | 2 bytes | 0 to 65,535 (unsigned character). |
short | 2 bytes | –32,768 to 32,767. |
ushort | 2 bytes | 0 through 65,535 (unsigned short ). |
int | 4 bytes | –2,147,483,648 to 2,147,483,647. |
uint | 4 bytes | 0 through 4,294,967,295 (unsigned integer). |
long | 8 bytes | –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. |
ulong | 8 bytes | 0 through 18,446,744,073,709,551,615 (unsigned long). |
decimal | 16 bytes | 0 to +/–79,228,162,514,264,337,593,543,950,335 with no decimal point. 0 to +/–7.9228162514264337593543950335 with 28 significant diits. |
float | 4 bytes | –3.4028235E+38 to –1.401298E-45 (negative values). 1.401298E–45 to 3.4028235E+38 (positive values). |
double | 8 bytes | –1.79769313486231570E+308 to –4.94065645841246544E–324 (negative values). 4.94065645841246544E–324 through 1.79769313486231570E+308 (positive values). |
string | varies | Depending on the platform, approximately 0 to 2 billion Unicode characters. |
DateTime | 8 bytes | January 1, 0001 0:0:00 to December 31, 9999 11:59:59 p.m. |
object | 4 bytes | Points to any type of data. |
(class) | varies | Class members have their own ranges. |
(structure) | varies | Structure members have their own ranges. |
You can use a cast to convert a value into a compatible data type. For example, the following code declares and initializes a long
variable. It then uses a cast to convert the long
into an int
and save the result in a new int
variable. The cast operator is highlighted in bold.
long longValue = 10;
int intValue = (int)longValue;
The Convert
class also provides methods for converting from one data type to another. The following table lists the most useful Convert
class functions.
Function | |
ToBoolean |
ToInt64 |
ToByte |
ToSByte |
ToChar |
ToSingle |
ToDateTime |
ToString |
ToDecimal |
ToUInt16 |
ToDouble |
ToUInt32 |
ToInt16 |
ToUInt64 |
ToInt32 |
All the Convert
class methods provide many overloaded versions to convert different kinds of values. For example, ToInt32
has different versions that take parameters that are bool
, byte
, string
, and other data types.
The integer functions ToInt16
, ToInt32
, ToInt64
, ToUInt16
, ToUInt32
, and ToUInt64
also provide overloaded versions that take as parameters a string value and a base, which can be 2, 8, 10, or 16 to indicate whether the string is in binary, octal, decimal, or hexadecimal, respectively. For example, the following statement converts the binary value 00100100 into the integer value 36.
int value = Convert.ToInt32("00100100", 2)
The BitConverter
class provides methods for converting variables to and from arrays of bytes.
In a widening conversion, the destination data type can hold any value held by the source data type. For example, a long
can hold any value that fits in an int
, so the following code is a widening conversion.
int intValue = 100;
long longValue = intValue;
Because this conversion must succeed, no casting or other operation is required.
In a narrowing conversion, the destination data type may not hold any value held by the source data type. For example, an int
cannot necessarily hold any value that fits in a long
, so the following code is a narrowing conversion.
long longValue = 100;
int intValue = (long)longValue;
Because this is a narrowing conversion, the casting operator is required, and the operation may throw an exception if the value cannot fit in the destination data type.
Converting an object into a less derived ancestor class is a widening conversion, so it doesn’t require casting. Conversely converting an object into a more derived descendant class is a narrowing conversion, so it requires casting.
For example, suppose the Employee
class is derived from the Person
class. Then the following code shows a widening conversion followed by a narrowing conversion.
Employee employee = new Employee();
Person person = new Person();
Employee employee2 = (Employee)person;
The as
operator provides a shorthand for converting objects from one type to another. The following code converts the value in variable person
into a Student
and saves it in variable student
.
student = person as Student;
If the value cannot be converted into a Student
(for example, if person
is a Janitor
), then the as
operator returns null
.
You can cast variables of array types. For example, an array of Student
objects is also an array of Person
objects because a Student
is a type of Person
. The following code demonstrates implicit and explicit array conversions.
// Make an array of Students.
Student[] students = new Student[10];
// Implicit cast to an array of Persons.
// (A Student is a type of Person.)
Person[] persons = students;
// Explicit cast back to an array of Students.
students = (Student[])persons;
Data types have parsing methods that convert string values into values of the data type. For example, the following code parses the text in the TextBox
named numValuesTextBox
and saves the result in the integer variable numValues
.
int numValues = int.Parse(numValuesTextBox.Text);
The data types also provide TryParse
methods that attempt to parse a string and return a boolean value indicating whether they succeeded.
18.118.208.97