Monday, December 19, 2005 10:21 PM Emile Bosch

Practical use of the operator "explicit" in XLinq

Probably everyone knows this already , but i am going to waste some energy on it anyway, as it raises way too many eyebrows lately :) As long as C# exists it supports the type conversion operator "explicit". Neverheless i've never seen any practical use on the operator before.  Probably because it has the tendency to release the "Huh?!1?" effect among developers. Until i started working with XLinq for my "Visual Fx" objectbuilder strategy. I wrote this little example to clarify:

public Example(XElement node)
{
    string str = (string)node.Attribute("Name"
); //Convert it to a string
    int number = (int)node.Attribute("Number"
);  //Convert it to an int
}

As you can see here, the Attribute property of type XAttribute can be casted to an String aswel to an Int (and several more types like DateTime, TimeSpan, Guid etc). Which is unusual since these types are not in the inheritence chain of XAttribute. This behaviour is conceived by using the "explicit" operator. By implementing this operator, which is easy,  you are responsible for converting the current type to your target type by doing so:

class XAttribute {
  //More really exciting code

  public static explicit operator string(XAttribute a)
  {
      if (a == null)
      {
            return null;
      }
      return a.value;
  }
}

Now this isn't exactly rocket science or anything but good to know when you encouter this behaviour in your new Xlinq applications. I've somewhat mixed feelings on this operator since there is no visual intellisense support for it available, thus there is no way of telling wheter you can simply cast an type using that operator or need to implement your own custom casting. Except for that minor point it certainly eases writing Xlink queries. Also, there is an "implicit" operator which does a  similiar thing except you don't have to write the cast explicitly, thus typing:

string str = node.Attribute("Name")

will suffice. (Note that the XAttribute doesnt implement the "implicit" operator so this won't compile)

More information on explicit and implicit on msdn: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfexplicit.asp

Filed under: ,

# re: Practical use of the operator "explicit" in XLinq

Tuesday, December 20, 2005 2:53 AM by Ramon Smits

Although I see some usefullness for it I don't recommend it at all. This is because casting will create a new object instead of a reference to the original object.

When you don't pass a reference but a new object then it is better to use a property or conversion method.

# re: Practical use of the operator "explicit" in XLinq

Tuesday, December 20, 2005 3:39 AM by Emile Bosch

Ramon, could you explain this to me? As far as i can see you gain more control by implementing your own operator, its then up to the developer wheter to return an new instance or a reference to it.

# re: Practical use of the operator "explicit" in XLinq

Wednesday, December 21, 2005 12:56 AM by Ramon Smits

Sure... Take a look at the msdn sample here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkuser-definedconversionstutorial.asp

The sample uses struct so they are value types. Pretend that they are reference types / objects.

Now the following sample:

------
RomanNumeral r1, r2;
r1 = 10;
r2 = r1;
r1 = 8;

Trace.Assert( (int)r1 == (int)r2, "The damn objects don't represent the same state." )
------

This is why I don't like them. If I would 'read' code and would see such a construction that I would expect that the state of the objects would be the same. But their not so let's not use (explicit) operators in such case.

Explicit operators are only interesting with valuetypes because you know that their behaviour is different. The MSDN samples much better then your sample here.

# re: Practical use of the operator explicit in XLinq

Wednesday, December 21, 2005 5:18 AM by Emile Bosch

First of all, the sample i've used is is directly taken from lutz roeders reflector source code on xlinq's Xattribute class, its not intended to be better than msdn' sample in any way. I do agree with the reading experience part, if you read this you could assume that r1 and r2 are the same value. Nevertheless, the behaviour you see here is expected. Even if you remove the (int) type conversion operator and make a property or a method out of it, the two values would still differ.

# re: Practical use of the operator "explicit" in XLinq

Thursday, December 22, 2005 12:30 AM by Teun

For value types, that behavior looks quite normal. I do agree with Ramon though that for reference types, implementing an explicit (or implicit) casting operator by returning a new instance could be confusing. Classes doing this should also have an appropriate Equals method implemented (and GetHashCode).

Leave a Comment

(required) 
(required) 
(optional)
(required)