Implicit operators in C# are fun!

Profile pictureToni Petrina
Published on 2017-01-181 min read

    Data conversion is boring, that is why people use AutoMapper. However, there is one other way for converting from one type to another - using implicit operator.

    Consider:

    public class Entity
    {
       public int Id { get; set; }
       public string Name { get; set; }
    }
    
    public class EntityDTO
    {
       public int Id { get; set; }
       public string Name { get; set; }
    }
    

    If we want separate classes in different layers (think persistence, domain, DTOs, view models) mapping can be hard. However, using implicit operators we can get a little more safety than usual.

    public class EntityDTO
    {
       public int Id { get; set; }
       public string Name { get; set; }
    
       public static implicit operator EntityDTO(Entity e)
          => new EntityDTO
          {
             Id = e.Id,
             Name = e.Name
          }
    }
    

    This allows us to write:

    var entity = context.Entities.Find(id);
    EntityDTO dto = entity;
    

    Is this fun? No. Is this easy? Again no. But at least we are in some control of conversion and if we rename or add properties to the source class, we will get compile time errors.


    Change code theme: