
Explicit provides conversion functionality. An explicit conversion involves casting from one type to another. With the explicit keyword, we implement the casting functionality as an operator method.
KeywordsThis C# article shows the explicit keyword in an example program. It then uses explicit conversions.

This program introduces two classes: the Apartment class and the House class. Each also provides a public static explicit operator: the Apartment provides a House operator, and the House provides an Apartment operator.
Program that uses explicit keyword [C#]
using System;
class Apartment
{
public string Name { get; set; }
public static explicit operator House(Apartment a)
{
return new House() { Name = a.Name };
}
}
class House
{
public string Name { get; set; }
public static explicit operator Apartment(House h)
{
return new Apartment() { Name = h.Name };
}
}
class Program
{
static void Main()
{
House h = new House();
h.Name = "Broadway";
// Cast a House to an Apartment.
Apartment a = (Apartment)h;
// Apartment was converted from House.
Console.WriteLine(a.Name);
}
}
Output
Broadway
Operator implementations. The explicit operators are implemented by constructing a new instance of the target type. They set the Name property to the Name property from the original instance. Thus the Apartment or House is now of the opposite type but has the same data field.
Operator OverloadingExplicit casting. Whenever you use a cast like (Apartment) or (House), this is an explicit cast. An implicit cast never uses the parentheses in its syntax. This example explicitly casts a House to an Apartment. Only one of the operators in the code is actually used.
Cast Examples
So what is the point of the explicit operator? It basically provides a special syntax form for converting types. This can be more intuitive for certain operations, mainly involving numeric types. For classes such as Apartment or House, an explicit operator is not normally needed or useful. The example here is for illustrative purposes.
Should I use it? Rarely.

As with the implicit keyword, the explicit keyword is used to implement conversions. You should be very careful with implementing conversions so that they are reversible and make sense. It is often better to implement conversions through helper methods so that the functionality is more obvious.
Implicit Keyword