C# Data Types

Data types in C# form the foundation of any application. 

Every variable you create has a specific data type that defines what kind of data it can store. 

Think of data types as the containers that hold different sorts of information. 

Just like you wouldn't store liquids in a box meant for dry goods, you wouldn’t use a variable type meant for integers to store a string of text. 

Let's explore these fundamental types and gain a clearer understanding of how they work.

What Are Data Types?

In simple terms, a data type specifies the kind of value a variable can hold. 

C# offers two main categories of data types: value types and reference types. 

Knowing the difference between them helps you manage how data is stored and manipulated in memory.

Value Types vs. Reference Types

  • Value Types: These types hold their data directly. When you assign a value type to another variable, a copy of that value is made. Common examples include int, float, and bool.
  • Reference Types: These types store a reference to the actual data. Instead of holding the data directly, they point to its location in memory. Examples include string, arrays, and custom objects.

Code Example

int a = 10; // Value type
int b = a;  // b is now 10, independent of a
a = 20;     // changing 'a' does not change 'b'
Console.WriteLine(b); // Outputs: 10

string str1 = "Hello"; // Reference type
string str2 = str1;    // str2 references the same string object as str1
str1 = "Goodbye";      // changing 'str1' does not change 'str2'
Console.WriteLine(str2); // Outputs: Hello

Common Value Types

C# provides several built-in value types. Here are a few of the most common:

Integer Types

  • int: Stores whole numbers from -2,147,483,648 to 2,147,483,647.
  • short: A smaller integer type, ranging from -32,768 to 32,767.
  • long: For larger integers, with a range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Floating-Point Types

  • float: A single-precision (32-bit) floating point.
  • double: A double-precision (64-bit) floating point.

Boolean Type

  • bool: This type can only hold true or false. It’s perfect for conditions and logic flows.

Code Example

int age = 30;
float temperature = 98.6f;
bool isRaining = false;

Console.WriteLine($"Age: {age}, Temperature: {temperature}, Is it raining? {isRaining}");

Common Reference Types

Just like value types, C# has several key reference types that you should know about.

String

The string type is often used to store text. Strings are immutable, meaning once created, their value cannot change.

Arrays

Arrays can store multiple values of the same type. 

For instance, an array can hold several integers, which can come in handy when dealing with lists of data.

Objects

C# allows you to create custom classes, and their instances are reference types. 

Objects can encompass a variety of data types and behaviors.

Code Example

string greeting = "Hello, World!";
string[] colors = { "Red", "Green", "Blue" };
var person = new { Name = "John", Age = 30 }; // Anonymous object

Console.WriteLine(greeting);
foreach (var color in colors)
{
    Console.WriteLine(color);
}
Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");

Nullable Types

In C#, sometimes you need to represent a variable that may not have a value (like an empty box). 

This is where nullable types come in. 

A nullable type can hold all the values of its underlying type plus an additional null value. 

You can declare these types using a question mark after the type name.

Example of Nullable Types

int? optionalValue = null;

if (optionalValue.HasValue)
{
    Console.WriteLine(optionalValue.Value);
}
else
{
    Console.WriteLine("Value is null");
}

Type Conversion

Understanding how to convert between different data types is crucial. 

This process can be implicit or explicit.

Implicit Conversion

This form of conversion happens automatically when C# safely converts one type to another without losing data.

Explicit Conversion

Also known as casting, explicit conversion happens when you convert a larger data type to a smaller one. When casting, there’s a risk of data loss.

Code Example

double pi = 3.14;
int wholePi = (int)pi; // Explicit conversion

Console.WriteLine(wholePi); // Outputs: 3
Previous Post Next Post

Welcome, New Friend!

We're excited to have you here for the first time!

Enjoy your colorful journey with us!

Welcome Back!

Great to see you Again

If you like the content share to help someone

Thanks

Contact Form