C# Data Types
In C#, data types are used to define the type of data that a variable can hold. C# provides a wide range of built-in data types, including integers, floating-point numbers, characters, and Boolean values. In this article, we'll explore the different C# data types and provide some examples of their usage.
C# Data Types
Here is a table of the different C# data types, including their type, size, and description:
Type | Size (bytes) | Description |
---|---|---|
sbyte | 1 | Signed 8-bit integer |
byte | 1 | Unsigned 8-bit integer |
short | 2 | Signed 16-bit integer |
ushort | 2 | Unsigned 16-bit integer |
int | 4 | Signed 32-bit integer |
uint | 4 | Unsigned 32-bit integer |
long | 8 | Signed 64-bit integer |
ulong | 8 | Unsigned 64-bit integer |
char | 2 | Single Unicode character |
float | 4 | Single-precision floating-point number |
double | 8 | Double-precision floating-point number |
decimal | 16 | Decimal number with 28-29 significant digits |
bool | 1 | Boolean value (true or false) |
object | Varies | Base class for all types in C# |
string | Varies | Sequence of Unicode characters |
Note that the size of some data types, such as object
and string
, may vary based on the contents of the variable.
Examples
Here are some examples of using different data types in C#:
1// integer 2int myInt = 42; 3 4// floating-point 5float myFloat = 3.14f; 6 7// character 8char myChar = 'A'; 9 10// Boolean 11bool myBool = true; 12 13// string 14string myString = "Hello, World!";
In these examples, we can see how different data types are used to hold different types of values. By understanding the different data types in C#, you can choose the appropriate type for your variables and make your code more efficient.