DEV Community

Sana Rana
Sana Rana

Posted on

How to Build AI-Powered .NET Apps with ML.NET and C#: A Step-by-Step Guide

Intro for Dev.to:
Artificial Intelligence is no longer limited to Python. With ML.NET, you can bring the power of machine learning directly into your C# and .NET applications — no need to switch tech stacks.
In this guide, I’ll walk you through what ML.NET is, why it’s a great fit for .NET developers, and how you can quickly get started building intelligent apps that can predict, classify, and analyze data right inside your C# codebase.

Whether you’re a beginner curious about AI or an experienced .NET dev looking to integrate machine learning, this post will give you the foundations you need.

What is ML.NET?
ML.NET is an open-source, cross-platform machine learning framework for .NET developers. It allows you to train, evaluate, and deploy custom ML models in C# or F#, without leaving your familiar environment.
Key benefits:

No Python required — all in C#
Cross-platform — runs on Windows, macOS, and Linux
Integrates with .NET apps seamlessly
Supports custom models and AutoML
Getting Started with ML.NET in C#

  • Install the Required Packages
dotnet add package Microsoft.ML
dotnet add package Microsoft.ML.Data

Enter fullscreen mode Exit fullscreen mode
  • Define Your Data Model
public class HouseData
{
    public float Size { get; set; }
    public float Price { get; set; }
}

public class HousePrediction
{
    public float Score { get; set; }
}
Enter fullscreen mode Exit fullscreen mode
  • Load and Train the Model
var context = new MLContext();
var data = context.Data.LoadFromTextFile<HouseData>(
    path: "housing.csv", separatorChar: ',', hasHeader: true);

var pipeline = context.Transforms.Concatenate("Features", new[] { "Size" })
    .Append(context.Regression.Trainers.Sdca(labelColumnName: "Price", maximumNumberOfIterations: 100));

var model = pipeline.Fit(data);

Enter fullscreen mode Exit fullscreen mode
  • Make Predictions
var prediction = context.Model.CreatePredictionEngine<HouseData, HousePrediction>(model)
    .Predict(new HouseData() { Size = 2500f });

Console.WriteLine($"Predicted price: {prediction.Score}");
Enter fullscreen mode Exit fullscreen mode

Real-World Use Cases
Predicting house prices (regression)
Classifying customer feedback (classification)
Detecting anomalies in system logs
Recommending products
Learn More & Full Guide
I’ve written a detailed step-by-step guide on how to build your first AI-powered .NET app with more examples and tips.

👉 Read the full article on Medium

Top comments (1)

Collapse
 
paweljanda profile image
Pawel Janda

It's great that you play with ML.NET! I also encourage you to give main.net a try. It's great for LLMs, both local and in the cloud. You can see some articles about it on my profile. Cheers!