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

Boxing effect on struct methods

Tom
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 value type like a reference type.
However, I have some questions relating to implicit boxing:

1. If I add custom instance method on a struct, will it box that type each
time the method is called? For example, suppose I have the following:

public struct MyStruct
{
private string _value;
public string Value
{
get {return _value;}
set {_value = value;}
}
public void DoInstanceStuff()
{
}
public static void DoStaticStuff()
{
}
}

MyStruct var;
var.DoInstanceStuff(); //<-- does this line box var?

2. Same question as #1 only relating to DoStaticStuff given that it is a
static method.

3. How do I substantiate the answer to either of the above questions?

4. If a value type is boxed, unboxed and then boxed again, does the system
reuse the original boxing's heap allocation or re-allocate a new area in the
heap. I'm presuming the later, but I wondered if the framework included
some sort of boxing optimization.

5. If I box a value type (explicit or implicit) and, while boxed, change one
of the value type's underlying field values (like through one of the value
type's methods), what does the system do?
Thanks,
Tom
Nov 16 '05 #1
1 2197
Tom <an******************@anywhere.com> wrote:
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 value type like a reference type.
However, I have some questions relating to implicit boxing:

1. If I add custom instance method on a struct, will it box that type each
time the method is called? For example, suppose I have the following:

public struct MyStruct
{
private string _value;
public string Value
{
get {return _value;}
set {_value = value;}
}
public void DoInstanceStuff()
{
}
public static void DoStaticStuff()
{
}
}

MyStruct var;
var.DoInstanceStuff(); //<-- does this line box var?
No.
2. Same question as #1 only relating to DoStaticStuff given that it is a
static method.
No.
3. How do I substantiate the answer to either of the above questions?
The easiest way to actually test it is to write some code, compile it,
and then use ILDASM to see what IL has been generated.
4. If a value type is boxed, unboxed and then boxed again, does the system
reuse the original boxing's heap allocation or re-allocate a new area in the
heap. I'm presuming the later, but I wondered if the framework included
some sort of boxing optimization.
I don't know for sure, but I would be slightly surprised if it did.
5. If I box a value type (explicit or implicit) and, while boxed, change one
of the value type's underlying field values (like through one of the value
type's methods), what does the system do?


The boxed value will change, but unless there's an explicit copy/unbox
in the IL stream, your normal variable won't see the change.

I don't know how you would do this in C# anyway, to be honest. Then
again, it's 4.30 in the morning, so I'm not at my brightest...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

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...
6
by: Justine | last post by:
Hi All, I need a small clarification with regard to Boxing. struct Currency { ......... } Currency Bal = new Currency; Object Obj = Bal;
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...
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...
5
by: Joe | last post by:
Consider the following code loop: for(int x = 0; x < 100; x++) { string sLoop = "Loop # " + (x+1).ToString(); Console.WriteLine(x); } I was told that the (x+1).ToString() was a boxing...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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: 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
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
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...

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.