Q re: Type casting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Woods

    Q re: Type casting

    Another quick q - sorry to trouble, again ...

    If I have a class as follows:

    Public Class MyClass(Of T)

    Private _myValue as T
    Public Property MyValue() as T
    Get
    Return _myValue
    End Get
    Set (value as T)
    _myValue = value
    End Set
    End Property

    Public Readonly Property MyValue1() as Object
    Get
    Return _myValue
    End Get
    End Property

    End Class

    Dim _myClass as New MyClass(Of String)

    What is happening, in terms of typecasting/performance (or perhaps
    wrapping), when I retrieve the _myValue out of the MyValue1 Getter.

    Thx

    Simon
  • Herfried K. Wagner [MVP]

    #2
    Re: Type casting

    "Simon Woods" <simonjwoods@ho tmail.comschrie b:
    Public Class MyClass(Of T)
    >
    Private _myValue as T
    Public Property MyValue() as T
    Get
    Return _myValue
    End Get
    Set (value as T)
    _myValue = value
    End Set
    End Property
    >
    Public Readonly Property MyValue1() as Object
    Get
    Return _myValue
    End Get
    End Property
    >
    End Class
    >
    Dim _myClass as New MyClass(Of String)
    >
    What is happening, in terms of typecasting/performance (or perhaps
    wrapping), when I retrieve the _myValue out of the MyValue1 Getter.
    You can examine the IL generated by the compiler using ILDASM which comes
    with the .NET Framework SDK or Lutz Roeder's Reflector for .NET. In the
    scenario you describe a simple (implicit) upcast to 'Object' is performed,
    which is not costly at all if the type is a reference type, which is the
    case for 'String'.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

    Comment

    • Simon Woods

      #3
      Re: Type casting

      Herfried K. Wagner [MVP] wrote:
      "Simon Woods" <simonjwoods@ho tmail.comschrie b:
      >Public Class MyClass(Of T)
      >>
      > Private _myValue as T
      > Public Property MyValue() as T
      >Get
      > Return _myValue
      > End Get
      >Set (value as T)
      > _myValue = value
      >End Set
      > End Property
      >>
      > Public Readonly Property MyValue1() as Object
      >Get
      > Return _myValue
      > End Get
      > End Property
      >>
      >End Class
      >>
      >Dim _myClass as New MyClass(Of String)
      >>
      >What is happening, in terms of typecasting/performance (or perhaps
      >wrapping), when I retrieve the _myValue out of the MyValue1 Getter.
      >
      You can examine the IL generated by the compiler using ILDASM which
      comes with the .NET Framework SDK or Lutz Roeder's Reflector for .NET.
      In the scenario you describe a simple (implicit) upcast to 'Object' is
      performed, which is not costly at all if the type is a reference type,
      which is the case for 'String'.
      >
      Excellent ... thanks for the tip

      Comment

      Working...