473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jun 27 '08 #1
2 838
"Simon Woods" <si*********@hotmail.comschrieb:
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/>

Jun 27 '08 #2
Herfried K. Wagner [MVP] wrote:
"Simon Woods" <si*********@hotmail.comschrieb:
>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
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Suzanne Vogel | last post by:
** Isn't the 'static_cast' operator the same as traditional type casting? ie, Aren't the following ways of getting b1, b2 the same? // 'Derived' is derived from 'Base' Derived* d = new...
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
3
by: Steve Teeples | last post by:
Can someone explain how to cast an object to a specific type during runtime? // This line of code tells me the objects type. System.Type type = System.Type.GetType("string of the type"); //...
10
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
10
by: lovecreatesbeauty | last post by:
Why (type*)pointer isn't equal to *(type**)pointer, In the code snippet, it shows that: (int *) == (int **) , (int *) != (*(int **)) . Does type-casting change the address? or...
6
by: crook | last post by:
I have code below and it works properly but when I'm compiling it with "--pedantic" flag, GCC(3.4.2) shows such warning: "ISO C forbids casting nonscalar to the same type". How can I change this...
15
by: shuisheng | last post by:
Dear All, Assume I have a class named Obj. class Obj { }; And a class named Shape which is derived from Obj. class Shape: public Obj
11
by: Frederic Rentsch | last post by:
Hi all, If I derive a class from another one because I need a few extra features, is there a way to promote the base class to the derived one without having to make copies of all attributes? ...
8
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.