473,608 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.DoInstanceS tuff(); //<-- 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 2214
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.DoInstanceS tuff(); //<-- 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.co m>
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
323
by: Lora Connors | last post by:
What is Boxing & UnBoxing in .NET?
43
6860
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...
6
1264
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
346
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 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...
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 ); .. ..
5
1751
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 statement, and would therefore cause a memory leak because there's no explicit unboxing
94
5648
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
13702
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
7777
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
8063
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8003
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
8498
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...
0
8478
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6817
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5476
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
3962
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
4025
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2474
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

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.