Question

I just saw it was using in one of the recent answers:

public static implicit operator bool(Savepoint sp)
{
    return sp != null;
}

Why do we need word implicit here, and what does it mean?

Was it helpful?

Solution

Implicit means that the conversion doesn't require a cast in your code.

You can now do this:

Savepoint point = new Savepoint();
if(point) // becomes a bool using your operator
{
}

instead of having to do this:

Savepoint point = new Savepoint();
if((bool)point) // an "explicit" conversion
{
}

One example of why this is a useful distinction is numeric types. There's an implicit conversion from "smaller" types to "larger" types, e.g:

float f = 6.5;
double d = f; // implicit conversion

But converting larger types to smaller types can be dangerous, so there's only an explicit conversion, forcing the user to clarify that he really intends to perform the operation in question:

long l = 20;
// short s = l;
short s = (short)l;  // explicit conversion

OTHER TIPS

That looks like misuse of an implicit operator. I don't know what the Savepoint class does, but converting it to a boolean does not seem logical. The implicit operator enables you to check if a Savepoint reference is null or not by simply evaluating the reference:

if (point) {

instead of:

if (point != null) {

If it's used that way, that is hiding what the code is actually doing, and that goes against pretty much everything that C# is about.

Just to add to mquander's answer.

From the C# 3.0 language specification:

A conversion operator declaration that includes the implicit keyword introduces a user-defined implicit conversion. Implicit conversions can occur in a variety of situations, including function member invocations, cast expressions, and assignments.

(section 10.10.3)

Conversion operators convert from one type to another. Implicit means you don't have to type an explicit cast in code for the conversion to happen, explicit requires that cast in code to be called.

You use implicit conversion operator when you are certain that the conversion does not lose any data. For example, with widening coercions (int -> long, float -> double). Explicit conversion operators are used when some state will be lost or you don't want compiler to automatically screw up client code by inserting conversions all over the place.

A conversion like yours would really wreak havoc because it is inadvisable to perform this particular conversion.

You need to specify 'implicit' because also there is explicit operators.

This means that conversion between Boolean and Savepoint can be done implicitly, e.g. = hidden.

Savepoint s = new Savepoint();
bool b = s;

Explicit conversion, e.g. evident, can be done this way:

 Savepoint s = new Savepoint();
 bool b = (bool)s;

Implicit conversion is often used when there is no possible data loss, i.e. Int16 (short) to Int32 (int). But if it's possible, you need to 'agree' with it using explicit conversion.

In your case, for example, if Savepoint is just a

struct Savepoint
{
  public bool data;
}

you can use implicit conversion, because no data loss is possible:

Savepoint s = new Savepoint();
s.data = true;
bool b = s;

otherwise I recommend you to define explicit operator!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top