Let's understand this with an example of converting Lead
to Opportunity
. If we define the Lead
as
public class Lead
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
and we want to convert it into Opportunity
as
public class Opportunity
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public decimal Worth {get;set;}
}
Map & Convert
In order to do that we need to create a new Instance of Opportunity
and then copy the values, so we created a new function to do that and this needs to be in a in a class
.
public Opportunity Convert(Lead lead)
{
Opportunity opp = new Opportunity();
opp.Id = lead.Id.ToString();
opp.FirstName = lead.FirstName;
opp.LastName = lead.LastName;
return opp;
}
So we move that method inside the Opportunity
class
as and make itstatic
so that it can be called as Opportunity.Convert()
:
public class Opportunity
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public decimal Worth {get;set;}
public static Opportunity Convert(Lead lead)
{
Opportunity opp = new Opportunity();
opp.Id = lead.Id.ToString();
opp.FirstName = lead.FirstName;
opp.LastName = lead.LastName;
return opp;
}
}
//main
void Main()
{
Lead lead = new Lead();
lead.Id = Guid.NewGuid();
lead.FirstName = "Vinod";
lead.LastName = "Srivastv";
lead.Dump();
Opportunity opp = Opportunity.Convert(lead);
opp.Dump();
}
But with explicit
and implicit
we can declare conversion operators so that we don't have to call the Convert
method of the Opportunity
class.
implicit
Example
In the implicit
conversion we don't need to cast the object
manually as it is done implicitly.
public class Opportunity
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public decimal Worth {get;set;}
public static implicit operator Opportunity(Lead lead)
{
Opportunity opp = new Opportunity();
opp.Id = lead.Id.ToString();
opp.FirstName = lead.FirstName;
opp.LastName = lead.LastName;
return opp;
}
}
//main
void Main()
{
Lead lead = new Lead();
lead.Id = Guid.NewGuid();
lead.FirstName = "Vinod";
lead.LastName = "Srivastv";
lead.Dump();
// here the lead is implicitly converted to opportunity
//and we get rid of the Convert Method from above
Opportunity opp = lead;
opp.Dump();
}
explicit
Example
Now we just made the change of the keyword from implicit to explicit from the code above:
public class Opportunity
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public decimal Worth {get;set;}
public static explicit operator Opportunity(Lead lead)
{
Opportunity opp = new Opportunity();
opp.Id = lead.Id.ToString();
opp.FirstName = lead.FirstName;
opp.LastName = lead.LastName;
return opp;
}
}
//main
void Main()
{
Lead lead = new Lead();
lead.Id = Guid.NewGuid();
lead.FirstName = "Vinod";
lead.LastName = "Srivastv";
lead.Dump();
//CS0266 Cannot implicitly convert type '.Lead' to 'Opportunity'.
//An explicit conversion exists (are you missing a cast?)
//Opportunity opp = lead;
//This is an example of explicit conversion, see the casting
Opportunity opp = (Opportunity)lead;
opp.Dump();
}
As we change the keyword the compiler will throw error as An explicit conversion exists (are you missing a cast?)
so we need to cast the Lead
object to opportunity with (Opportunity)
as above.