C Sharp implicit casting

DBJ dreaming in C# while lying on green grass
DBJ dreaming in C# while lying on the green grass

C Sharp implicit casting

February 13th, 2008

There might be something very interesting for you in FM#. Maybe a interesting example of the usage, of C# implicit casting operator? … Wait, what is FM#?

FM# is (my) C# library of Foundation Mechanisms (aka FM).

For a start, let me offer some magic. Lets look in the method cast() of the util class.  This method is used to get the value from the xml node, and cast it implicitly, to the C# variable of the same type. Reminder: value of the xml node is always an string.  Confusing. Let’s just use this then. Here is the method footprint:

MIT (c) by DBJ.ORG
public static Implicitor cast(
       XmlNode xmlnode, 
       string name, 
       params object [] fallback


Where “xmlnode” is the root node of the xml fragment to be used, “name” is the tag name of the required child node. “fallback” is optional argument used as a fallback value, optionally given if sub-node by given name is not found.

Return value is (of course)  interesting because it is an instance of the Implicitor class, also from the FM#  library.  This  method in effect “automagically” converts (xml) string values into the type of variable receiving the result of this method.  This is done through the magic of the Implicitor, which is the actual return type. Here is how.

// These are both legal calls
// find a first node called ‘wait’ under the endpoint_setting
// return it’s string value and cast it into the ‘int’ type
// this ‘magic’ can be done for any type 
// that Implicitor supports.
// get the value of the “wait” XML node as int and 
// assign it to he variable “wait”
// endpoint_setting contains an XML fragment: <wait>”100″”</wait>
int wait = DBJ.corelib.util.cast( endpoint_setting, “wait” ) ;
// another example: get the value of the “progid” node as a string
string progid = DBJ.corelib.util.cast( endpoint_setting, “progid” ) ;
// what actually happens ‘implicitly’ is this
corelib.Implicitor temporary = 
     DBJ.corelib.util.cast( endpoint_setting, “wait” ) ;
        int wait = temporary ;
// where ‘temporary’ is made by C# compiler and is ‘invisible’
// Fallback value may be optionaly given.
// Exception is NOT thrown in this case
// UNLESS fallout type is different than the required result type!
string progid =
endpoint_setting, “progid”, “DEFAULT.PROGID”
) ;
// Following line throws the exception from the Implicitor
// because the fallout value is not ‘int’
// which is a type ‘implicitly’ required
int wait = DBJ.corelib.util.cast(
endpoint_setting, “wait”, “Has to be ‘int’”
) ;

In essence Implicitor is a class which transforms implicitly an object (given to its constructor) into the type of the variable on the left side of the assignment statement , aka “=”.

I am sure VB people will call this “VARIANT” type. Here we have the similar container of a single type less value, with implicit casting added. To make it useful. Here is the Implicitor use in its simplest form.

Implicitor i = new Implicitor( 13 ) ;
int v = i ;
// above line makes implicit use of this operator
// on the Implicitor class
// public static implicit operator int (Implicitor ep) ;
// compiler automatically selects this operator
// because of the type of ‘v’. therefore
string wrong = i;
// inevitably throws an exception

Above will work because “V” is of the correct type. An “int”. same as the type referenced inside the Implicitor, instance “i” above. This might seem as not a big deal … untill you see and understand its usage and potential, as in the util.cast() method, described first above.