C# — Data Types
Type categories
| Category | Types |
|---|---|
| Value types | int, double, bool, char, struct, enum |
| Reference types | string, array, class, interface, delegate |
Value types
using System;
// Integers
byte b = 255; // 1 byte
short s = -32000; // 2 bytes
int i = 2147483647; // 4 bytes
long l = 9223372036854775807L; // 8 bytes
nint ni = 42; // Native int (C# 9+)
// Unsigned
uint ui = 4294967295U;
ulong ul = 18446744073709551615UL;
// Floating-point
float f = 3.14f; // 4 bytes
double d = 3.14159; // 8 bytes (default)
decimal m = 3.14159m; // 16 bytes (precise)
// Other
bool flag = true; // 1 byte
char c = 'A'; // 2 bytes (Unicode)
Reference types
using System;
// String (immutable)
string name = "Hello";
string upper = name.ToUpper();
// Array
int[] nums = { 1, 2, 3, 4, 5 };
string[] names = new string[3];
// Object
object obj = 42; // Boxing
int unboxed = (int)obj; // Unboxing
Console.WriteLine(name);
Console.WriteLine(nums.Length);
Structs
using System;
struct Point
{
public double X;
public double Y;
public Point(double x, double y)
{
X = x;
Y = y;
}
public double DistanceTo(Point other)
{
double dx = X - other.X;
double dy = Y - other.Y;
return Math.Sqrt(dx * dx + dy * dy);
}
}
var p1 = new Point(0, 0);
var p2 = new Point(3, 4);
Console.WriteLine($"Distance: {p1.DistanceTo(p2)}"); // 5
Enums
using System;
// Basic enum
enum Color { Red, Green, Blue }
// Flags enum
[Flags]
enum Permissions
{
None = 0,
Read = 1,
Write = 2,
Execute = 4,
All = Read | Write | Execute
}
var favorite = Color.Green;
var perms = Permissions.Read | Permissions.Write;
Console.WriteLine(favorite); // Green
Console.WriteLine(perms); // Read, Write
Tuples
using System;
// Named tuple
var point = (X: 3.0, Y: 4.0);
Console.WriteLine($"({point.X}, {point.Y})");
// Unnamed tuple
(int, string) person = (30, "Alice");
// Method returning tuple
static (int min, int max, double avg) GetStats(int[] numbers)
{
return (numbers.Min(), numbers.Max(), numbers.Average());
}
var stats = GetStats(new[] { 1, 2, 3, 4, 5 });
Console.WriteLine($"Min: {stats.min}, Max: {stats.max}, Avg: {stats.avg}");
Collections
using System;
using System.Collections.Generic;
// List<T>
var numbers = new List<int> { 1, 2, 3, 4, 5 };
numbers.Add(6);
numbers.RemoveAt(0);
Console.WriteLine($"Count: {numbers.Count}");
// Dictionary<TKey, TValue>
var ages = new Dictionary<string, int>
{
["Alice"] = 30,
["Bob"] = 25
};
Console.WriteLine($"Alice: {ages["Alice"]}");
// HashSet<T>
var unique = new HashSet<int> { 1, 2, 3, 2, 1 };
Console.WriteLine($"Unique count: {unique.Count}"); // 3
// Queue<T>
var queue = new Queue<string>();
queue.Enqueue("First");
queue.Enqueue("Second");
Console.WriteLine(queue.Dequeue()); // First
// Stack<T>
var stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
Console.WriteLine(stack.Pop()); // 2
Type aliases
using System;
// Keywords are aliases for .NET types
int i = 42; // Same as System.Int32
string s = "Hi"; // Same as System.String
bool b = true; // Same as System.Boolean
Console.WriteLine(typeof(int)); // System.Int32
Console.WriteLine(typeof(string)); // System.String
Mini Practice
Write C# code that:
- Creates a
structwith fields and methods - Uses a tuple to return multiple values from a method
- Creates and manipulates a
List<int>and aDictionary<string, int> - Demonstrates enum with
[Flags]attribute
Up Next
In the next lesson, you'll learn about Operators — arithmetic, comparison, and logical operations.
Related Topics
Frequently Asked Questions about Data Types
What is Data Types in C#?
Data Types is a fundamental concept in C#. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Data Types?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Data Types.
Why is Data Types important in C#?
Data Types is essential for C# development. Understanding this concept will help you write better code and solve real-world problems more effectively.