explicit and implicit c#

The implicit and explicit keywords in C# are used when declaring conversion operators. Let's say that you have the following class:

public class Role
{
    public string Name { get; set; }
}

If you want to create a new Role and assign a Name to it, you will typically do it like this:

Role role = new Role();
role.Name = "RoleName";

Since it has only one property, it would perhaps be convenient if we could instead do it like this:

Role role = "RoleName";

This means that we want to implicitly convert a string to a Role (since there is no specific cast involved in the code). To achieve this, we add an implicit conversion operator:

public static implicit operator Role(string roleName)
{
    return new Role() { Name = roleName };
}

Another option is to implement an explicit conversion operator:

public static explicit operator Role(string roleName)
{
    return new Role() { Name = roleName };
}

In this case, we cannot implicitly convert a string to a Role, but we need to cast it in our code:

Role r = (Role)"RoleName";

In general

  • Implicit: something is being done for you automatically.
  • Explicit: you've written something in the source code to indicate what you want to happen.

For example:

int x = 10;
long y = x; // Implicit conversion from int to long
int z = (int) y; // Explicit conversion from long to int

Implicit and explicit are used quite a lot in different contexts, but the general meaning will always be along those lines.

Note that occasionally the two can come together. For instance:

int x = 10;
long y = (long) x; // Explicit use of implicit conversion!

(An explicit conversion is one which has to be stated explicitly; an implicit version is one which can be used implicitly, i.e. without the code having to state it.)


Consider you have two classes:

internal class Explicit
{
    public static explicit operator int (Explicit a)
    {
        return 5;
    }
}


internal class Implicit
{
    public static implicit operator int(Implicit a)
    {
        return 5;
    }
}

and two objects:

var obj1 = new Explicit();
var obj2 = new Implicit();

you can now write:

int integer = obj2; // implicit conversion - you don't have to use (int)

or:

int integer = (int)obj1; // explicit conversion

but:

int integer = obj1; // WON'T WORK - explicit cast required

Implicit conversion is meant to be used when conversion doesn't loose any precision. Explicit conversion means, that you can loose some precision and must state clearly that you know what you're doing.

There is also a second context in which implicit/explicit terms are applied - interface implementation. There are no keywords in that case.

internal interface ITest
{
    void Foo();
}

class Implicit : ITest
{
    public void Foo()
    {
        throw new NotImplementedException();
    }
}

class Explicit : ITest
{
    void ITest.Foo() // note there's no public keyword!
    {
        throw new NotImplementedException();
    }
}

Implicit imp = new Implicit();
imp.Foo();
Explicit exp = new Explicit();
// exp.Foo(); // won't work - Foo is not visible
ITest interf = exp;
interf.Foo(); // will work

So when you use explicit interface implementation, interface's methods are not visible when you use concrete type. This can be used when interface is a helper interface, not part of class'es primary responsibility and you don't want additional methods to mislead someone using your code.