03 Jan 23

public class DomainModel {

public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }

}

public class Dto {

public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }

public static explicit operator Dto(DomainModel model)
{
    return new Dto
    {
        Id = model.Id,
        Name = model.Name,
        Description = model.Description
    }
}

}

var domainModel = new DomainModel {

Id = 1,
Name = "Example",
Description = "This is an example"

}

var dto = (Dto)domainModel;

by ciwchris 3 years ago