cSharp Articles
C# Files C# Enums C# Interfaces C# Abstraction C# polymorphism C# inheritance guide C# access modifiers c# constructors C# class members C# class objects C# method overloading C# return values c# methods C# array sorting C# arrays C# forEach loop C# strings C# user input c# data type C# variables whats C#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
, andbool
. - 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 holdtrue
orfalse
. 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