Loading...

C# Basic Data Types

Basic data types in C#: numeric, text, logical, object-based, and nullable types.

In C#, data is stored using different types, and each data type defines how much memory is used and which operations can be performed. These types play a key role in how a program behaves. In this article, we will explore numeric, textual, logical, dynamic, and nullable data types with practical examples.

Choosing the right data type is a crucial part of learning C#. Proper type selection improves performance, optimizes memory usage, and helps prevent common programming errors. Understanding the fundamental data types early on provides a solid foundation for building reliable applications.


Numeric Types

Numeric types are used for integers and floating-point numbers. For financial calculations, decimal is recommended.

Type Description Example Memory
intInteger (−2B - +2B)424 bytes
longLarger integers1000000008 bytes
shortSmall integers1202 bytes
byte0 - 255 positive2001 byte
doubleFloating-point (double precision)3.148 bytes
floatFloating-point (single precision)2.5f4 bytes
decimalDecimal for financial use19.99m16 bytes

int age = 25;
double pi = 3.14159;
decimal price = 19.99m;

Console.WriteLine(age);
Console.WriteLine(pi);
Console.WriteLine(price);

Note: For financial calculations, decimal is preferred over double because it avoids floating-point rounding errors and provides higher precision.


Text Types

char holds a single character, while string holds a sequence of characters.

TypeDescriptionExample
charSingle character'A', '9'
stringText / sequence of characters"Hello World"

char firstLetter = 'M';
string message = "Hello";

Console.WriteLine(firstLetter);
Console.WriteLine(message);

Logical Type

bool can only hold true or false. It is widely used in conditions.


bool isStudent = true;
bool isGraduate = false;

Console.WriteLine(isStudent);
Console.WriteLine(isGraduate);

Object and Dynamic Types

object, var, and dynamic are used for flexible usage.


object data = 42;
var text = "Hello";
dynamic variable = 5;
variable = "Now it's a string";

Console.WriteLine(data);
Console.WriteLine(text);
Console.WriteLine(variable);

Nullable Types

Value types (e.g. int, double, bool) cannot be null by default. Adding ? allows them to be nullable.


int? grade = null;

if (grade == null)
    Console.WriteLine("No grade entered.");
else
    Console.WriteLine($"Grade: {grade}");

When Should Each Data Type Be Used?


Assignments Between Data Types and Type Conversions

In C#, variables of different data types cannot always be assigned to each other directly. Attempting to assign incompatible types results in a compile-time error.

Invalid Assignments


int number = 10;
string text = number; // ERROR

bool flag = true;
int value = flag;     // ERROR

In the examples above, the assignments fail because the data types are not compatible.

C# type conversion error in Visual Studio IntelliSense

In the image above, Visual Studio immediately detects invalid assignments between incompatible data types. The example shows attempts to assign values between types such as bool and int, which are not directly compatible. The editor highlights the issue with a red underline and displays a compile-time error like CS0029.

Implicit Conversions


int small = 5;
double large = small; // Valid

Smaller numeric types can be automatically converted to larger numeric types.

Explicit Conversions


double pi = 3.14;
int wholeNumber = (int)pi; // 3

Conversions that may cause data loss or precision loss require explicit casting.


Summary