3️⃣ C# Variables, Data Types & Type Systems
Estimated reading: 3 minutes 399 views

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

CategoryExamplesNotes
Value Typesint, float, bool, charStored on the stack
Reference Typesstring, arrays, classes, objectsStored on the heap
Nullable Typesint?, bool?Value types that can be null
User-defined Typesclass, struct, enum, interfaceBuilt by developers

Built-in Value Types

TypeSizeExampleDescription
int4 bytesint age = 30;Integer (whole number)
float4 bytesfloat pi = 3.14f;Single-precision decimal
double8 bytesdouble e = 2.718;Double-precision decimal
char2 byteschar grade = 'A';Single character
bool1 bytebool isValid = true;Boolean true/false

Best Practice: Use double for precision math and int for counters or indexes.


Reference Types

TypeDescription
stringSequence of characters
objectBase type of all types in .NET
ArraysFixed-size collections like int[] numbers
ClassCustom 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

FeatureValue TypeReference Type
Memory LocationStackHeap
Default BehaviorCopy by valueCopy by reference
Null AssignmentNot allowed (unless nullable)Allowed
PerformanceFaster for small dataEfficient 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 counters
  • string for user input, messages, file names
  • bool for toggles, status flags
  • char 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, and bool 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 :
Share

πŸ“¦ C# Data Types

Or Copy Link

CONTENTS
Scroll to Top