473,594 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is Boxing and UnBoxing Really Necessary?

Exactly why does C# and .NET require all the extra
copying of data?
Nov 17 '05 #1
8 1717
Boxing / casting are part of the downside of an early-bound,
strongly-typed environment. If all types must be known at compile time,
then you end up using object or some other common ancestor to hold
instances whose type is not known at compile time, and then you either
have to live with the reduced functionality of the ancestor or cast it
to something more useful based on some runtime decision process.

By contrast in a late bound, loosely-typed environment the runtime may
not care what an object *is* so much as what it *does*. For example if
I make a call like someObject.Fuba r(), a late bound runtime may just try
to make the Fubar() call, throwing an exception only if that method
doesn't exist in someObject.

The trade off is that early binding / strong typing gives us the
benefits of type safety and (on balance anyway) better performance
because all addresses can be resolved statically at compile time rather
than dynamically at runtime.

The 2.0 version of the CLR (VS 2005) provides a mechanism called
generics that gets around some of this fuss. For example rather than
creating an ArrayList of objects to hold some variable number of ints
you can create a List<int> and the runtime will build a strongly typed
list of ints under the hood based on the template of the generic List
class. You can then get ints in and out of the collection with no
casting / boxing / unboxing, and yet the generic List<> class is
reusable for any kind of object your might desire and will work with
them all, without the need for boxing.

--Bob

Peter Olcott wrote:
Exactly why does C# and .NET require all the extra
copying of data?

Nov 17 '05 #2

"Bob Grommes" <bo*@bobgrommes .com> wrote in message news:O7******** *****@tk2msftng p13.phx.gbl...
Boxing / casting are part of the downside of an early-bound, strongly-typed environment. If all types must be known at compile
time, then you end up using object or some other common ancestor to hold instances whose type is not known at compile time, and
then you either have to live with the reduced functionality of the ancestor or cast it to something more useful based on some
runtime decision process.

By contrast in a late bound, loosely-typed environment the runtime may not care what an object *is* so much as what it *does*.
For example if I make a call like someObject.Fuba r(), a late bound runtime may just try to make the Fubar() call, throwing an
exception only if that method doesn't exist in someObject.
What I am talking about is that C++ can have value-types and reference-types
on both the stack and the heap. Also conversion can be made between
value-types and reference-types without the run-time overhead of copying
the whole thing.

int X = 5;
int* ptrX = &X;
int Z = *X;

I am not completely sure, but I think that the above conversions
between value-type X and Z, and reference-type ptrX, have
no run-time overhead at all. I don't see why C# can do something
like this (working with pointers) instead of requiring that all the
underlying data be copied. If this was a large struct rather than
a simple int, then the extra cost can become quite large.
The trade off is that early binding / strong typing gives us the benefits of type safety and (on balance anyway) better
performance because all addresses can be resolved statically at compile time rather than dynamically at runtime.

The 2.0 version of the CLR (VS 2005) provides a mechanism called generics that gets around some of this fuss. For example rather
than creating an ArrayList of objects to hold some variable number of ints you can create a List<int> and the runtime will build a
strongly typed list of ints under the hood based on the template of the generic List class. You can then get ints in and out of
the collection with no casting / boxing / unboxing, and yet the generic List<> class is reusable for any kind of object your might
desire and will work with them all, without the need for boxing.
Ah, now that's more like it.
--Bob

Peter Olcott wrote:
Exactly why does C# and .NET require all the extra
copying of data?

Nov 17 '05 #3

"Peter Olcott" <ol****@att.net > wrote in message
news:pd3Se.7432 $UI.6710@okepre ad05...
I don't see why C# can do something
like this (working with pointers) instead of requiring that all the
underlying data be copied. If this was a large struct rather than
a simple int, then the extra cost can become quite large.


Just a guess, but I'd imagine it has to do with reference counting and
garbage collection. If you have untyped pointers all over the place it
becomes very difficult (impossible?) to GC the memory. I could be wrong, but
that's my guess.
Nov 17 '05 #4
I can argue that C++ classes do a heck of lot more copying that C# since
C++
classes use value semantics and the copy constructor gets called
everywhere like
rabbits.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #5

"Jeff Louie" <je********@yah oo.com> wrote in message news:uU******** *****@tk2msftng p13.phx.gbl...
I can argue that C++ classes do a heck of lot more copying that C# since
C++
classes use value semantics
The difference is that with C++ one has the choice of using
reference semantics instead of value semantics, whenever
desired. With C# it seems that one is limited to the requirements
of the framework.
and the copy constructor gets called
everywhere like
rabbits.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #6

"Scott Roberts" <sc***********@ no-spam.intelebill .com> wrote in message news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..

"Peter Olcott" <ol****@att.net > wrote in message
news:pd3Se.7432 $UI.6710@okepre ad05...
I don't see why C# can do something
like this (working with pointers) instead of requiring that all the
underlying data be copied. If this was a large struct rather than
a simple int, then the extra cost can become quite large.


Just a guess, but I'd imagine it has to do with reference counting and
garbage collection. If you have untyped pointers all over the place it
becomes very difficult (impossible?) to GC the memory. I could be wrong, but
that's my guess.

There is no reason why they must be untyped pointers.
The example that I provided was an int (thus typed) pointer.
Nov 17 '05 #7

"Peter Olcott" <ol****@att.net > wrote in message
news:pd3Se.7432 $UI.6710@okepre ad05...

"Bob Grommes" <bo*@bobgrommes .com> wrote in message
news:O7******** *****@tk2msftng p13.phx.gbl...
Boxing / casting are part of the downside of an early-bound,
strongly-typed environment. If all types must be known at compile time,
then you end up using object or some other common ancestor to hold
instances whose type is not known at compile time, and then you either
have to live with the reduced functionality of the ancestor or cast it to
something more useful based on some runtime decision process.

By contrast in a late bound, loosely-typed environment the runtime may
not care what an object *is* so much as what it *does*. For example if I
make a call like someObject.Fuba r(), a late bound runtime may just try to
make the Fubar() call, throwing an exception only if that method doesn't
exist in someObject.


What I am talking about is that C++ can have value-types and
reference-types
on both the stack and the heap. Also conversion can be made between
value-types and reference-types without the run-time overhead of copying
the whole thing.

int X = 5;
int* ptrX = &X;
int Z = *X;

I am not completely sure, but I think that the above conversions
between value-type X and Z, and reference-type ptrX, have
no run-time overhead at all. I don't see why C# can do something
like this (working with pointers) instead of requiring that all the
underlying data be copied. If this was a large struct rather than
a simple int, then the extra cost can become quite large.


Consider this

public void Method()
{
int test = 5;
BoxedParameterM ethod(test);
}

public void BoxedParameterM ethod(object o)
{
classField = o;
}

object classField;

If you passed a pointer to test to the method BoxedParameterM ethod, the
pointer would point to test's allocation on the stack frame of the method
Method. After that method invocation returns that stack frame will cease to
exist and your program is no longer correct, classField points to random
data. The only way to circumvent this would be to allocate space and copy
test on the heap, which is basically what boxing does.

There are other reasons, as well. Boxed objects get a vtable pointer in
their header, allowing the use of interfaces. They can also be treated as
objects, you can lock on them(although its *VERY* unadvisable to do so),
compare them referentially, and in general treat them as full .NET reference
types.

With a pointer,you get none of that. You have to have access to a vtable to
work with interfaces, and since value types do not carry vtable pointers,
you'd lose access to instances in untyped scenarios. Locking is impossible
since the basic locking system relys on sync blocks in the object header.
Without converting the structure instance on the stack into a full reference
type, you lose all of this and you start making it possible to make an
"object" variable not an "object" anymore, such as in the above example.
Nov 17 '05 #8

"Peter Olcott" <ol****@att.net > wrote in message
news:q78Se.7447 $UI.4519@okepre ad05...

"Scott Roberts" <sc***********@ no-spam.intelebill .com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..

"Peter Olcott" <ol****@att.net > wrote in message
news:pd3Se.7432 $UI.6710@okepre ad05...
I don't see why C# can do something
like this (working with pointers) instead of requiring that all the
underlying data be copied. If this was a large struct rather than
a simple int, then the extra cost can become quite large.


Just a guess, but I'd imagine it has to do with reference counting and
garbage collection. If you have untyped pointers all over the place it
becomes very difficult (impossible?) to GC the memory. I could be wrong,
but
that's my guess.

There is no reason why they must be untyped pointers.
The example that I provided was an int (thus typed) pointer.


Yes there is.

public void Method(object o);

is different semantically from

public void Method(int* o);

The first acts upon any object, be it an integer, a string, or
MyRandomFileTyp e.

int* only works on pointers to integers. Boxing isn't used to reference a
local variable's storage location, you use ref and out parameters for that,
its used to convert a value instance into an object so it can be treated as
an object.
Nov 17 '05 #9

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

Similar topics

43
6859
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's another story.) The data is written once and then read many times. Each primitive read requires unboxing. The data reads are critical to overall app performance. In the hopes of improving performance, we have tried to find a way to avoid the...
24
2598
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When you pass a Value-Type to method call ,Boxing and Unboxing would happen,Consider the following snippet: int a=1355; myMethod(a); ......
16
1617
by: Ed A | last post by:
Hi all: I'm using a struct of type Point that is being passed on to a method as a refrence type (boxing), the UpdatePoint method changes the coordinates of this point as such: Point p = new Point(); UpdatePoint( P ); .. ..
4
2962
by: Peter Olcott | last post by:
I want to be able to make my .NET applications run just as fast as unmanaged C++. From my currently somewhat limited understanding of the .NET framework and the C# language, it seems that Boxing/Unboxing might present of problem. Since C++ has pointer syntax, I was thinking that this might eliminate the need for Boxing and Unboxing. Am I right? One of the things that my application needs is something exactly like std::vector<unsigned...
94
5646
by: Peter Olcott | last post by:
How can I create an ArrayList in the older version of .NET that does not require the expensive Boxing and UnBoxing operations? In my case it will be an ArrayList of structures of ordinal types. Thanks.
19
13700
by: ahjiang | last post by:
hi there,, what is the real advantage of boxing and unboxing operations in csharp? tried looking ard the internet but couldnt find any articles on it. appreciate any help
161
7770
by: Peter Olcott | last post by:
According to Troelsen in "C# and the .NET Platform" "Boxing can be formally defined as the process of explicitly converting a value type into a corresponding reference type." I think that my biggest problem with this process is that the terms "value type" and "reference type" mean something entirely different than what they mean on every other platform in every other language. Normally a value type is the actual data itself stored in...
0
7876
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8251
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...
1
8003
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
8234
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5408
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3859
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3897
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2385
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
0
1210
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.