01.Blogs :
Didith  
A blog about the stuff that keeps life interesting, meaningful, and fun.

C# questions

Can you describe a situation in which you would actually make use of boxing and unboxing?  I understand the concept but I'm struggling to think of a situation in which that would require me to use these.

Also, why would you use a struct instead of a class? 

posted on Tuesday, April 25, 2006 8:14 AM by Didith

# re: C# questions @ Monday, April 24, 2006 10:43 PM

In .Net Class Library we have a type called ArrayList. As the name says, it's an array like a list.

To add data to an object of type ArrayList it's necessary to use its method Add. This method has a parameter of type Object (the main type of the .Net Framework).

Suppose that you have added several string variables to an ArrayList. As its method supports the object type as a parameter, you don't need to do explicit boxing.

But if you need to retrieve one of the items in the ArrayList, you would use the Item property of the ArrayList, that returns a value of type object.

So if you need to store this value in a string variable, you'll need to do unboxing to model the value from object to string.

More detail about boxing and unboxing: http://www.csharphelp.com/archives/archive100.html

juliano_netfox

# re: C# questions @ Monday, April 24, 2006 11:38 PM

Thanks for this.

Didith

# re: C# questions @ Tuesday, April 25, 2006 3:06 AM

I recently discovered/learned of the use of Generics for using in creating lists, aside from ArrayLists. It's cool coz you can actually place ANY kind of datatype onto a list (note, not an array). Try it out ma'am. A sample application from the VS2005 CD I was giving out during the send-off has that applied.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/csharp_generics.asp

Aids

# re: C# questions @ Tuesday, April 25, 2006 4:38 AM

Hi Maam,

Structs are used when you just have to encapsulate data elements as in data types like when you are making a Point struct or something similar. Also copying this copies values...unlike classes which pass references (uh hahaha..tama pa ba pinagsasabi ko dito).

Classes, when you need to encapsulate everything else with methods as well :D Can refer you to this link.

http://www.codeproject.com/csharp/BoxUnBox.asp?df=100&forumid=97265&exp=0&select=929129
http://www.codeproject.com/csharp/structs_in_csharp.asp

she_lim

# re: C# questions @ Tuesday, April 25, 2006 4:59 AM

yes she, it's more or less what structs do, contain the values

Aids


 
03.UPDATE CALENDAR :
<April 2006>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

05.MY LINKS :

07.Subscriptions :

Subscriptions


© Copyright 2005 Microsoft Corporation. All Rights Reserved.
Terms of Use | Privacy Statement | Code of Conduct | Hosted by MaximumASP for Microsoft
WHO-BAR