473,414 Members | 1,980 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,414 software developers and data experts.

Is Boxing and UnBoxing Really Necessary?

Exactly why does C# and .NET require all the extra
copying of data?
Nov 17 '05 #1
8 1704
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.Fubar(), 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*************@tk2msftngp13.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.Fubar(), 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@okepread05...
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********@yahoo.com> wrote in message news:uU*************@tk2msftngp13.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****************@TK2MSFTNGP10.phx.gbl...

"Peter Olcott" <ol****@att.net> wrote in message
news:pd3Se.7432$UI.6710@okepread05...
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@okepread05...

"Bob Grommes" <bo*@bobgrommes.com> wrote in message
news:O7*************@tk2msftngp13.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.Fubar(), 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;
BoxedParameterMethod(test);
}

public void BoxedParameterMethod(object o)
{
classField = o;
}

object classField;

If you passed a pointer to test to the method BoxedParameterMethod, 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@okepread05...

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

"Peter Olcott" <ol****@att.net> wrote in message
news:pd3Se.7432$UI.6710@okepread05...
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
MyRandomFileType.

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
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...
24
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...
16
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...
4
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...
94
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. ...
19
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
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...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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.