[Mono-list] C# question

Dan Guidara dan@guidara.com
11 Jan 2003 20:30:38 +0000


On Fri, 2003-01-10 at 19:46, dietmar wrote:
> Maybe someone can explain that behaviour?
> 
> - Dietmar
> 
> -----------------------------------
> using System;
> 
> public class Test {
> 
> 	public enum MyEnum {
> 		ZERO,
> 		ONE
> 	}
> 
> 	public static int Main() {
> 		MyEnum en = MyEnum.ONE;
> 		IComparable ic;
> 
> 		/* this works */
> 		ic = (IComparable)(object)en;
> 		
> 		/* compiler error ??*/
> 		ic = (IComparable)en;
> 				
> 		return 0;
> 	}
> }
> 
> 
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list@ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
> 

As far as I understand it, all objects in C# are inherited from class
object. Therefore every single object can be implicited casted to it,
(its the base class). Now, the enum is downcasted to an object then
casted as a Icomparable. It stands to reason that something of type
object can be convert into any of its child types, I am sure that
strange behavior occurs from this sometimes.  The second piece of code
is trying to go directly from an Enum to an Icomplarable through an
implicit cast which isnt doable given the type differences.

HTH,
Dan