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:

TypeSize (bytes)Description
sbyte1Signed 8-bit integer
byte1Unsigned 8-bit integer
short2Signed 16-bit integer
ushort2Unsigned 16-bit integer
int4Signed 32-bit integer
uint4Unsigned 32-bit integer
long8Signed 64-bit integer
ulong8Unsigned 64-bit integer
char2Single Unicode character
float4Single-precision floating-point number
double8Double-precision floating-point number
decimal16Decimal number with 28-29 significant digits
bool1Boolean value (true or false)
objectVariesBase class for all types in C#
stringVariesSequence 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.