[Mono-list] implicit, explicit, and why does C# have these?

Todd Berman tberman@gentoo.org
Fri, 17 Oct 2003 09:39:45 -0400

Actually, I must say that I am glad the implicit/explicit operators are

For example, let's say you have a class that does a lot of stuff, but
also contains a Uri. Now, it is nice to be able to say if(UriContainer
=3D=3D someUri) without having to worry about the casting.

Or, for a real world example, take the WSE2 Addressing class Action. It
is a class that represents an XmlElement (Note, this class does *not*
inherit from an XmlElement at all). The InnerText of this xml element is
a string that contains the 'action' of the Soap Addressing Header. With
implicit operators it is possible to do this:

	switch(ActionObject) {
		case "urn:test:action1":
			//Your code here.
		case "urn:test:action2":
			//Your code here.

Admittedly, there are for sure other ways to accomplish the same goal,
but none are nearly as readable as that.

Now, the language could have been set up to just make all operators
implicit, and again, I am glad they didn=92t do that, because it allows =
as a API programmer to construct my API in such a fashion that its
somewhat self documenting to the end developer.


-----Original Message-----
From: mono-list-admin@lists.ximian.com
[mailto:mono-list-admin@lists.ximian.com] On Behalf Of David La Motta
Sent: October 17, 2003 9:23 AM
To: Jonathan Pryor
Cc: Mono-List
Subject: Re: [Mono-list] implicit, explicit, and why does C# have these?

Thanks for the explanation.=A0 I can see how the implicit operator can =
useful in the example you describe; still, I think it wasn't necessary
for C# to expose them to us.=A0 I.e., let us deal with the explicit =
and spare the confusion they may cause.=A0 In other words, an implicit
cast from a Pear object to a Truck object can seem quite odd, assuming
their inheritance tree has nothing in common.

// David

Jonathan Pryor wrote:

It's good to keep this in mind: C#'s "builtin" types (int, long, etc.)
are actually aliases for managed types (System.Int32, System.Int64,
etc.).  These managed types are (for Mono, at least) written in C#.

You expect the following code to work:

	int n =3D 42;
	long l =3D n;
	short s =3D (short) n;

Which means that the following code also works:

	System.Int32 n =3D 42;
	System.Int64 l =3D n;
	System.Int16 s =3D (System.Int16) n;

Which means that the managed implementation of those managed types needs
*some way* to represent to the compiler/runtime that some coercions are
"safe" (can be done implicitly), while others are "unsafe" (can be done

C#'s implicit/explicit operators are how this is declared and defined.

As for the differences between implicit & explicit, you understand the
difference already.  Implicit =3D=3D doesn't need a cast; explicit =
requires a cast.

 - Jon

On Thu, 2003-10-16 at 16:52, David La Motta wrote:
So I was reading my "C# for Java Developers" book and I came across the=20
implicit and explicit operators.  Java doesn't have these but a friend=20
suggests C++ does.  Being that I am not a C++ developer I really can't=20
comment much on it, except to say that it looks like C# just decided to=20
copy functionality from C++, just because it is a "cute" feature of the=20
language.  I also can't quite put my finger on the difference between=20
implicit and explicit, so if anybody has any insight on these, please,=20
do share.

It seems to me that explicit is used when you want to force your API=20
clients to use a cast when dealing with different types.  So lets say=20
that I have:

public static implicit operator Foo(Bar bar) {...}    and I also have a=20
method called
public Foo morph(Foo foo) {...}

If I was ever to use my morph method with a Bar, I could issue the call=20

Bar bee =3D new Bee();
Foo faa =3D morph(bee);

And the compiler would be happy.  If I was to change implicit for=20
explicit in the operator's declaration, the way of calling the method=20
would be:

Foo faa =3D morph((Foo) bee);   // with explicit cast

Is this it, or is there more to it than this?  I also am aware that this

isn't really a "mono" question per se, but I thought some of you would=20
be willing to shed some light on the topic...  :-)


// David

Mono-list maillist  -  Mono-list@lists.ximian.com