Curriculum
In C#, a data type is a classification of data that determines the possible values and operations that can be performed on that data. There are several built-in data types in C#, including numeric, Boolean, character, and string types. In this tutorial, we’ll explore these data types in detail, including their syntax, examples, and rules.
Numeric data types are used to represent numbers in C#. There are several different numeric data types, each with its own range and precision.
Integer types represent whole numbers that can be positive, negative, or zero. The following table shows the integer data types in C# along with their range and size:
Type | Range | Size |
---|---|---|
sbyte | -128 to 127 | 1 byte |
byte | 0 to 255 | 1 byte |
short | -32,768 to 32,767 | 2 bytes |
ushort | 0 to 65,535 | 2 bytes |
int | -2,147,483,648 to 2,147,483,647 | 4 bytes |
uint | 0 to 4,294,967,295 | 4 bytes |
long | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 8 bytes |
ulong | 0 to 18,446,744,073,709,551,615 | 8 bytes |
Here’s an example of declaring and assigning an integer variable:
int myNumber = 42;
Floating-point types are used to represent numbers with fractional parts. The following table shows the floating-point data types in C# along with their range and precision:
Type | Range | Precision |
---|---|---|
float | ±1.5 x 10^-45 to ±3.4 x 10^38 | 7 digits |
double | ±5.0 x 10^-324 to ±1.7 x 10^308 | 15-16 digits |
decimal | ±1.0 x 10^-28 to ±7.9 x 10^28 | 28-29 digits |
Here’s an example of declaring and assigning a double variable:
double myDouble = 3.14;
C# also has several other numeric data types, including char
and bool
. char
is used to represent a single character, while bool
is used to represent a Boolean value (true
or false
).
C# also has several non-numeric data types, including:
The Boolean data type is used to represent logical values. It can have two possible values: true
or false
. Here’s an example of declaring and assigning a Boolean variable:
bool myBoolean = true;
The character data type is used to represent a single character. It is denoted by the char
keyword and is represented by a single character enclosed in single quotes. Here’s an example of declaring and assigning a char variable:
char myChar = 'a';
The string data type is used to represent a sequence of characters. It is denoted by the string
keyword and is represented by a series of characters enclosed in double quotes. Here’s an example of declaring and assigning a string variable:
string myString = "Hello, world!";
The object data type is the base class for all other data types in C#. It can hold values of any type and is denoted by the object
keyword. Here’s an example of declaring and assigning an object variable:
object myObject = "Hello, world!";
Nullable types are used to represent a value type that can be assigned a null
value. Value types are normally non-nullable, meaning they cannot be assigned a null value. To make a value type nullable, you can use the ?
modifier. Here’s an example of declaring and assigning a nullable integer variable:
int? myNullableInt = null;
Here are some rules for declaring data types in C#:
Here’s an example of declaring and initializing a variable:
int myNumber = 42;
In this example, we declare a variable named myNumber
of type int
and initialize it to the value 42
.
Type conversion is the process of changing a value from one data type to another. In C#, there are two types of conversions: implicit and explicit.
Implicit conversion happens automatically and without data loss when a value of a smaller data type is assigned to a variable of a larger data type. For example:
int myInt = 42; long myLong = myInt;
In this example, we assign an int
value to a long
variable, and C# automatically converts the int
value to a long
value.
Explicit conversion requires a cast operator and may result in data loss if the value being converted is too large for the target data type. For example:
double myDouble = 3.14159; int myInt = (int)myDouble;
In this example, we cast a double
value to an int
value, and any decimal places are truncated.
Constants are variables that hold values that cannot be changed once they are assigned. They are declared using the const
keyword and must be initialized with a value at the time of declaration. Here’s an example of declaring a constant:
const double PI = 3.14159;
In this example, we declare a constant named PI
of type double
and initialize it to the value 3.14159
. Once a constant is declared and initialized, it cannot be changed.
In this tutorial, we learned about the different data types in C# and how to declare and initialize variables of those types. We also learned about type conversion, which is the process of changing a value from one data type to another, and how to declare and use constants. By understanding the rules for declaring and using data types, we can write programs that are more efficient and less error-prone.