π¦ C# Data Types β Understanding Built-in and Custom Types
π§² Introduction β Why Data Types Matter in C#
Every piece of data in C# has a data type that determines how much memory it takes, how it’s stored, and what operations can be performed on it. Understanding C# data types is essential for writing type-safe, efficient, and error-free code.
π― In this guide, youβll learn:
- The main categories of data types in C#
- Differences between value types and reference types
- How to use and declare each type
- Common pitfalls and best practices
π Core Concept β What Are C# Data Types?
A data type defines the kind of data a variable can store. C# supports a variety of built-in types and also allows the creation of custom types using classes, structs, and enums.
π Categories of C# Data Types
Category | Examples | Notes |
---|---|---|
Value Types | int , float , bool , char | Stored on the stack |
Reference Types | string , arrays, classes, objects | Stored on the heap |
Nullable Types | int? , bool? | Value types that can be null |
User-defined Types | class , struct , enum , interface | Built by developers |
π’ Built-in Value Types
Type | Size | Example | Description |
---|---|---|---|
int | 4 bytes | int age = 30; | Integer (whole number) |
float | 4 bytes | float pi = 3.14f; | Single-precision decimal |
double | 8 bytes | double e = 2.718; | Double-precision decimal |
char | 2 bytes | char grade = 'A'; | Single character |
bool | 1 byte | bool isValid = true; | Boolean true/false |
π Best Practice: Use double
for precision math and int
for counters or indexes.
π Reference Types
Type | Description |
---|---|
string | Sequence of characters |
object | Base type of all types in .NET |
Arrays | Fixed-size collections like int[] numbers |
Class | Custom reference type |
π§ Note: Reference types store a pointer to the memory location.
π» Code Example β Using Different Data Types
using System;
class DataTypeDemo
{
static void Main()
{
int age = 28;
double salary = 55000.75;
char grade = 'A';
bool isEmployed = true;
string name = "John";
Console.WriteLine($"Name: {name}, Age: {age}, Grade: {grade}, Salary: {salary}, Employed: {isEmployed}");
}
}
π€ Output:
Name: John, Age: 28, Grade: A, Salary: 55000.75, Employed: True
π§ Value Types vs Reference Types
Feature | Value Type | Reference Type |
---|---|---|
Memory Location | Stack | Heap |
Default Behavior | Copy by value | Copy by reference |
Null Assignment | Not allowed (unless nullable) | Allowed |
Performance | Faster for small data | Efficient for large/complex data |
π‘ Tips, Pitfalls & Best Practices
π‘ Tip: Use var
when the type is obvious, but avoid overusing it.
π Best Practice: Choose the right type based on the required precision and memory needs.
β οΈ Pitfall: Using float
or double
for money calculations may lead to rounding errorsβuse decimal
instead.
π οΈ Use Cases β Where Data Types Are Used
int
,double
for calculations and countersstring
for user input, messages, file namesbool
for toggles, status flagschar
for character data (e.g., grades, initials)- Custom classes/structs for structured data
π Summary β Recap & Next Steps
C# provides a rich set of data types for efficient and flexible data storage. Knowing which type to use helps you write cleaner, faster, and safer code.
π Key Takeaways:
- Value types are stored on the stack; reference types on the heap
- Use
decimal
for money,string
for text, andbool
for logic - Understand how types affect performance and memory
βοΈ Coming up: Learn how to safely convert between types using π¦ C# Type Casting / Type Conversion
β FAQ β C# Data Types
β What are the main data types in C#?
β
C# includes value types (e.g., int
, float
) and reference types (e.g., string
, object
, arrays).
β What is the default value of a C# data type?
β
For value types: 0
for numbers, false
for bool
, '\0'
for char
. For reference types: null
.
β What type should I use for currency?
β
Use decimal
for high-precision financial calculations.
β Can I create my own data types in C#?
β
Yes. You can define custom classes, structs, enums, and interfaces.
β What is the base type of all C# types?
β
The object
type is the base class for all types in C#.
Share Now :