Manually Mapping Objects in C#
Sometimes, using libraries like AutoMapper can feel like overkill — especially for small projects or when you want full control over the mapping process.
Get Osama Haider’s stories in your inbox
Join Medium for free to get updates from this writer.
In this blog, I’ll show you how to manually map objects from one class to another in a simple and easy way.
✅ When Should You Do Manual Mapping?
- When you only have a few mappings.
- When you want better readability and control.
- When you want to avoid extra dependencies like AutoMapper.
Step-by-Step: Create a Project and Do Manual Mapping
📁 Project Name: ManualObjectMapping
Step 1: Create the Project
If you’re using the terminal:
dotnet new console -n ManualObjectMapping
cd ManualObjectMappingStep 2: Create the User Class
File: User.cs
public class User
{
public int Id { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
}Step 3: Create the UserDto Class
File: UserDto.cs
public class UserDto
{
public string Name { get; set; }
public string Email { get; set; }
}Step 4: Create the Mapper Class
File: Mapper.cs
public static class Mapper
{
public static UserDto MapToDto(User user)
{
return new UserDto
{
Name = user.FullName,
Email = user.Email
};
}
}Step 5: Use the Mapper in Program.cs
File: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
var user = new User
{
Id = 1,
FullName = "Osama Haider",
Email = "osama@example.com"
};
UserDto userDto = Mapper.MapToDto(user);
Console.WriteLine($"Name: {userDto.Name}, Email: {userDto.Email}");
// Bonus: Mapping a list
List<User> users = new List<User>
{
new User { Id = 1, FullName = "Ali", Email = "ali@example.com" },
new User { Id = 2, FullName = "Sara", Email = "sara@example.com" }
};
List<UserDto> userDtos = users.Select(Mapper.MapToDto).ToList();
foreach (var dto in userDtos)
{
Console.WriteLine($"Name: {dto.Name}, Email: {dto.Email}");
}
}
}Final Thoughts
Manual mapping is simple, clean, and gives you full control. For small and medium projects, it’s often the best approach. No need to overcomplicate with AutoMapper if you don’t have to.
🔔 Follow for more simple C# and .NET tips!
👉 Connect with me on LinkedIn for more real-world coding insights, clean examples, and .NET goodness.







