Skip to content
Tutorials Center
My learning

Topics

Lesson 41 of 42 ยท Projects and Practice

Project: Expense Tracker

Build a .NET MAUI expense tracker: model money correctly with decimal, group spending by category and month, set budgets, export CSV and draw a simple chart, with runnable C# core logic.

An expense tracker looks simple and hides most real-app problems: money must never be stored as double, dates need time zones and months, reports need grouping, and users expect charts and exports. This project builds the domain logic first (runnable here), then shows how to display it with MAUI controls and GraphicsView.

The MAUI pages and the SQLite package cannot run on this site; XAML and device code are read-only. All the calculations (totals, budgets, grouping, CSV) are plain C# and run in the examples.

Features

  • Add an expense: amount, category, date, note.
  • See this month's total, a list grouped by day and a breakdown by category.
  • Set a monthly budget per category and show how much is left.
  • Export the month as CSV and share it.
  • Show a bar chart of the categories.

Model money correctly

Binary floating point cannot store 0.1 exactly, so sums drift:

C# Why money is never a double

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Use decimal for amounts, in SQLite store either decimal as text or an integer count of the smallest unit (cents). Pick one rule for rounding and apply it everywhere. Remember that multi-currency apps also need the currency code with every amount.

Models

C#
public enum Category { Food, Transport, Home, Fun, Health, Other }

public class Expense
{
    [PrimaryKey, AutoIncrement] public int Id { get; set; }
    public decimal Amount { get; set; }
    public Category Category { get; set; }
    public DateTime Date { get; set; }
    public string Note { get; set; } = "";
}

Store Date as a plain local date (DateOnly or the date part), since "I bought coffee on Tuesday" does not shift when you travel to another time zone. Timestamps for sync, on the other hand, should be UTC.

Reports

All reports are LINQ queries over expenses. This stand-in builds the monthly report, the budget status and a text bar chart:

C# Core logic: monthly report, budgets and chart

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Group by Category for the pie or bar chart, by Date for the list (a CollectionView with IsGrouped="True" shows these as sections), and compare against budgets for the progress bars.

CSV export and sharing

CSV looks trivial but needs escaping: a note may contain commas or quotes. The rule is to wrap such fields in quotes and double the quotes inside.

C# Core logic: a correct CSV export

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Always format numbers with CultureInfo.InvariantCulture in files. A German phone would otherwise write 12,50, and the comma would break the format. In the app, write the file to FileSystem.CacheDirectory and share it:

C#
string path = Path.Combine(FileSystem.CacheDirectory, "expenses-2026-05.csv");
await File.WriteAllTextAsync(path, csv);
await Share.Default.RequestAsync(new ShareFileRequest("Expenses", new ShareFile(path)));

Drawing the chart

For a small bar chart you do not need a chart library. GraphicsView with an IDrawable draws with the cross-platform canvas:

C#
public class BarChartDrawable : IDrawable
{
    public List<(string Label, float Value)> Bars { get; set; } = new();

    public void Draw(ICanvas canvas, RectF area)
    {
        if (Bars.Count == 0) return;
        float max = Bars.Max(b => b.Value);
        float slot = area.Width / Bars.Count;

        for (int i = 0; i < Bars.Count; i++)
        {
            float height = area.Height * 0.8f * Bars[i].Value / max;
            canvas.FillColor = Colors.SteelBlue;
            canvas.FillRoundedRectangle(i * slot + 8, area.Height - height - 20, slot - 16, height, 4);
            canvas.FontColor = Colors.Gray;
            canvas.DrawString(Bars[i].Label, i * slot, area.Height - 16, slot, 16, HorizontalAlignment.Center, VerticalAlignment.Center);
        }
    }
}
XML
<GraphicsView x:Name="chart" HeightRequest="200" />

Set chart.Drawable = new BarChartDrawable { Bars = ... } and call chart.Invalidate() when data changes. The scaling rule (height = maxHeight * value / max) is the same as the text bars above, which is why it is worth getting right in testable code first.

Adding an expense

The form page uses Entry with Keyboard="Numeric", a Picker for the category and a DatePicker. Parsing the amount must respect the user's culture (they type 12,50 on many phones), so parse with the current culture and validate:

C#
public bool TryParseAmount(string text, out decimal amount)
    => decimal.TryParse(text, NumberStyles.Number, CultureInfo.CurrentCulture, out amount) && amount > 0;

Extension ideas

  1. Recurring expenses (rent, subscriptions) generated on a schedule.
  2. Receipt photos stored in the app data folder.
  3. Multiple currencies with a stored exchange rate per expense.
  4. A yearly report and a month-over-month comparison.
  5. Backup and restore of the database file.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which type should store money amounts?

    Binary floating point cannot represent values like 0.1 exactly, so totals drift. decimal or integer cents are exact.
  2. 2. Why use CultureInfo.InvariantCulture when writing numbers to CSV?

    Culture-specific formatting can output 12,50, which breaks a comma separated file. Invariant formatting is stable.
  3. 3. How is a field that contains a comma written in CSV?

    This is the standard CSV escaping rule that all readers understand.
  4. 4. Which LINQ operator creates the 'spending by category' report?

    GroupBy collects expenses by category and Sum totals each group.

Summary

  • Store money as decimal (or integer cents) and choose one rounding rule.
  • Reports are LINQ: Where for the month, GroupBy and Sum for categories and days, comparison with budgets.
  • Export CSV with correct escaping and InvariantCulture, then share the file.
  • A GraphicsView with an IDrawable draws simple charts; get the scaling logic right first.

Next: Practice: Exercises and Interview Questions