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

Boxing m.m

Hello!

I have three questions.
1. When is it useful to use boxing for value types.
2. I have earlier been using C and C++ for many years but the last year I
have been using C#.
In C# you can use pointer if you use unsafe directive but I can't see any
purpose to use the unsafe directive
with pointer. Is it right that the use of unsafe code with pointer is of no
use?
3.When you use the unboxing is it always nessesary that the type in the box
is always the same as the cast type.
An example
int i = 23;
object o = i;
i = (int)o;
//Tony
Dec 27 '07 #1
2 1137

"Tony Johansson" <jo*****************@telia.comwrote in message
news:g9*****************@newsb.telia.net...
Hello!

I have three questions.
1. When is it useful to use boxing for value types.
Pretty much never, now that we have type-safe generic collections.
2. I have earlier been using C and C++ for many years but the last year I
have been using C#.
In C# you can use pointer if you use unsafe directive but I can't see any
purpose to use the unsafe directive
with pointer. Is it right that the use of unsafe code with pointer is of
no use?
No, but it is never necessary. You may get better performance with
pointers, then unsafe code would be useful.
3.When you use the unboxing is it always nessesary that the type in the
box is always the same as the cast type.
An example
int i = 23;
object o = i;
i = (int)o;
That's right, you must first unbox by casting to the original type, then you
can convert.

int i = 23;
object o = i;
short s = (short)(int)o; // ok
s = (short)o; // runtime error
>

//Tony

Dec 27 '07 #2
Ben Voigt [C++ MVP] <rb*@nospam.nospamwrote:

<snip>
3.When you use the unboxing is it always nessesary that the type in the
box is always the same as the cast type.
An example
int i = 23;
object o = i;
i = (int)o;

That's right, you must first unbox by casting to the original
type, then you can convert.
Note that there are a few oddities around this in reality, which are
due to the semantics of the CLR. I can't remember many off the top of
my head, other than enums:

using System;

class Test
{
enum Fred : int
{
Foo,
Bar
}

static void Main()
{
Fred x = Fred.Bar;
object o = x;
int i = (int)o; // This works

Console.WriteLine(i);
}
}

But yes, it's a good idea to always unbox to the original type - that
should *always* work :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 27 '07 #3

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

Similar topics

14
by: Lora Connors | last post by:
What is Boxing & UnBoxing in .NET?
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...
1
by: Tom | last post by:
Couple of questions relating to boxing. Firstly, I already know that boxing is the processing of temporarily copying a ValueType (e.g. struct, enum) to the heap so that the system can treat a...
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...
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...
14
by: dave.dolan | last post by:
Basically I'd like to implement the composite design pattern with leaves that are either of reference or value types, but even using generics I can't seem to avoid boxing (using ArrayList or...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.