HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to C#VariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Dictionary
🔀 Condition & Loop
🧩 Function
C#›Data Types

Data Types

int, double, string, bool; string interpolation ($) and text↔number conversion.

Simple enough for a 5-year-old to understand

Basic types and interpolation

You can embed values into text by writing {ifade} inside $"..." (string interpolation).

Example code
1
using System;
2
int number = 7;
Integer.
VariableInteger.
3
double decimalNum = 3.14;
Decimal.
VariableDecimal.
4
string text = "C#";
Text.
VariableText.
5
Console.WriteLine($"{number} {decimalNum} {text}");
It prints 7 3.14 C#.
Output / InputIt prints 7 3.14 C#.
VariableOutput / Input
💡 Hint: Put the $ sign before the quote: $"{name}".

About this topic

C#'s type system has two main layers. Value types hold their values directly and their contents are copied when assigned. Reference types point to an object, and when copied, only the pointer is copied — the object is shared.

The choice of numeric types is based on practical rules: int for integers, long for very large numbers, double for decimals, and decimal for MONEY. decimal is a key advantage of C#: a type designed for financial calculations that stores decimals in base-10.

The inability of every value type to hold null was a problem; nullable value types solved this. If a database column can be empty, its counterpart should be a nullable type.

What you will learn

  • Distinguish between value and reference types
  • Choose numeric types and understand the importance of decimal
  • Perform explicit and implicit type conversion
  • Convert strings to numbers using Parse and TryParse
  • Integer division behavior
  • Use nullable value types

When to use?

When converting user input to a number, use TryParse instead of Parse: Parse throws an error on invalid input, while TryParse returns false and keeps the program running. It is the standard approach for form and console input.

Use decimal for money, rate, and tax calculations. double accumulates penny losses and creates unacceptable discrepancies in financial reports.

Common mistakes

Wrong

int a = 7;
int b = 2;
Console.WriteLine(a / b);

The division of two integers is an integer; the result is 3, not 3.5, and the decimal part is truncated. There is no error message, the calculation is silently wrong. At least one side must be a decimal.

Correct

int a = 7;
int b = 2;
Console.WriteLine((double) a / b);

Wrong

string input = "abc";
int value = int.Parse(input);

Parse throws "FormatException: The input string 'abc' was not in a correct format" on invalid text and the program crashes. TryParse should always be used for user input.

Correct

string input = "abc";
if (int.TryParse(input, out int value)) {
    Console.WriteLine(value);
} else {
    Console.WriteLine("invalid number");
}

Wrong

double price = 19.99;
double total = price * 3;
Console.WriteLine(total == 59.97);

double stores decimals approximately in binary; the product won't be exactly 59.97 and the comparison returns false. Using decimal for money calculations completely eliminates this class of errors.

Correct

decimal price = 19.99m;
decimal total = price * 3;
Console.WriteLine(total == 59.97m);

Frequently asked questions

What is the difference between double and decimal?

double stores decimals approximately in binary, is fast, and suitable for scientific calculations. decimal stores in base-10, is much more precise, and is designed for money. decimal must be used in every financial calculation; the letter m is appended to the number.

How do I choose between Parse and TryParse?

Use Parse if you are sure the input is valid; use TryParse if it comes from a user, file, or network. TryParse does not throw errors, tells you whether the conversion succeeded as a boolean, and this is the correct way to work with external data.

What is the difference between struct and class?

struct is a value type, copied when assigned. class is a reference type, the same object is shared when assigned. struct is used for small, immutable data pieces, while class is used for objects that carry behavior and have identity — in practice, class is much more common.

Summary

In C#, value types are copied, reference types are shared. Integer division truncates decimals, TryParse is used for user input, and decimal is mandatory for money calculations.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code