473,404 Members | 2,137 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,404 software developers and data experts.

implicit autoboxing

hi!

i always hear about the dangers (performance loss) that comes with implicit
autoboxing. when are situations where this happens?

i only know this happens when i assign a value type to a variable of type
object.

int i = 1234;
object obj = i; // here the implicit conversion takes place

void Foo(object o){}
Foo( i ); // here too

additionally are there situations of implicit unboxing where programmers
should take care of?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #1
5 4582
i always hear about the dangers (performance loss) that comes with implicit
autoboxing. when are situations where this happens?
Assigning a value type variable to an implemented interface also
causes it to be boxed.

additionally are there situations of implicit unboxing where programmers
should take care of?


No, unboxing is made explicitly.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
thanks for reply.

interfaces are object types too, so i wouldn't wonder about that.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
> Assigning a value type variable to an implemented interface also
causes it to be boxed.


and what about

struct S : ICountable{};

S s;
int i = s.Count; // is this a boxing?

this is imho an implicit conversion to an implemented interface. but does
this mean, that boxing occures?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #4


cody wrote:
Assigning a value type variable to an implemented interface also
causes it to be boxed.

and what about

struct S : ICountable{};

S s;
int i = s.Count; // is this a boxing?

this is imho an implicit conversion to an implemented interface.


How? There's no implicit conversion happening anywhere. Interfaces only
provide an interface that should be implemented - structs implementing
interfaces do not imply that structs (value types) implicitly converted
to a reference type.
but does this mean, that boxing occures?
No. Count (I'm guessing) is an integer (and so a value type) and you're
simply copying its value to the variable 'i'. If, in some way, Count is
an object, the line won't compile. Boxing only occurs when you try
assigning a value type to a reference type.
-Andre

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk


Nov 15 '05 #5

"cody" <de********@gmx.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Assigning a value type variable to an implemented interface also
causes it to be boxed.


and what about

struct S : ICountable{};

S s;
int i = s.Count; // is this a boxing?

this is imho an implicit conversion to an implemented interface. but does
this mean, that boxing occures?


No, boxing does not occur. Calls only go through the interface when you call
through the interface. If you wrote:

S s;
ICountable ic = s;
int i = ic.Count;

Then there would be boxing.

Another way to look at it is that boxing only occurs when you assign a value
type to object or to an interface.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 15 '05 #6

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

Similar topics

2
by: Oliver Plohmann | last post by:
Hello! In JDK1.5 Sun has introduced autoboxing. The user does not need to convert an instance of Integer into an int herself anymore when she wants to do arithmetic operations with the int. The...
3
by: Reneé | last post by:
I wanted to know the order of implicit conversions and which sort of values allow them. From searching around in books and the archive of this mailing list, it seems to be that only numbers are...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
9
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
18
by: Tony | last post by:
class Interface { public: virtual void DoItNow()=0; }; class A: public Interface { public: void DoItNow(); // satisfies interface explicitly
13
by: java4all | last post by:
public class Test { public static void main(String args) { HashSet s = new HashSet(); for(short i = 0; i<100;i++){ s.add(i); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.