473,659 Members | 3,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4610
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******** ********@TK2MSF TNGP12.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
3041
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 compiler takes care of the required conversion. I like this approach since it preserves the efficiency of primitive data types but lets the user believe she were dealing with objects. Now it would be nice to be consequent and move the methods...
3
2615
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 implicitly converted within each other and bools can be implicitly converted to ints? However, I'm unable to find any other implicit conversions and the order of the implicit conversions (something like int->float->long). Any help would be greatly...
11
7606
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 Test. I expected the same error from the following line, but it compiles fine. The double is silently truncated to an int and then fed in to the implicit conversion operator. Why does this happen? Is there any way that I can keep the implicit...
9
2076
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 emulate the pattern im trying to follow in an existing project. These are my steps: 1) I have a method "printMe" existing in the application which originally used to take in a string. This method is static and sits in the Driver
11
12761
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
36
3618
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 in a C# program but not VB? -- Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/ "Programming is an art form that fights back"
17
2497
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
18
2380
by: Tony | last post by:
class Interface { public: virtual void DoItNow()=0; }; class A: public Interface { public: void DoItNow(); // satisfies interface explicitly
13
4696
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); s.remove(i-1); } System.out.println(s.size()); } }
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7359
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.