Hi Mattias,
thank you for your answer.
I don't think this is compiler feature for 2 reasons:
1) i know that .NET framework resolvs Calls for overloaded methods (with
object parameter) at run-time and not at compile time
(
http://msdn.microsoft.com/library/de...bspec9_3_4.asp)
2) i have tried same test with VB.NET with same results. It is a vb.net
compiler feature too ?!
Thanks in advance.
this is VB.NET code i tried:
Imports System
Module Module1
Sub Main()
Dim mytestclass0 As New TestClass(0) 'Constructor with
SimpleEnum was called !
Dim mytestclass1 As New TestClass(1) 'Constructor with object
was called !
Dim mytestclassneg1 As New TestClass(-1) 'Constructor with object
was called !
Dim mytestclass1000 As New TestClass(1000) 'Constructor with object
was called !
Dim mytestclassStr As New TestClass("a") 'Constructor with object
was called !
Console.WriteLine("Why this behaviour ?!")
Console.ReadLine()
'Why 0 is a special value ?
End Sub
End Module
Class TestClass
Public Enum SimpleEnum
Red = 0
Blue = 1
Yellow = 2
End Enum
Public Sub New(ByVal se As SimpleEnum)
Console.WriteLine("Constructor with SimpleEnum was called !")
End Sub
Public Sub New(ByVal obj As Object)
Console.WriteLine("Constructor with object was called !")
End Sub
End Class
you wrote:
"reason is that the literal 0 is implicitly convertible to any enum type".
Ok ... but why this behaviour only for 0 (zero) and not for 1 (one) !?
They are both int !
"Mattias Sjögren" wrote:
>
> >My question is: why 0 (zero) is a special value in CLR ?!
>
> This isn't a CLR issue. It's a C# compiler feature, since method
> overload resolution is performed at compile time.
>
> And the reason is that the literal 0 is implicitly convertible to any
> enum type.
>
>
>
> Mattias
>
> --
> Mattias Sjögren [MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
>