I am writing an app that might largely benefit from using a struct instead
of a class. However, it needs to be rich enough where there are methods
available.
My question is - every time you call a method on a struct, is it boxed?
Because that would be a huge perf hit.
I am assuming that it does have to be boxed, but if anyone knows for sure
that would be great! 10 1251
Nathan, I am writing an app that might largely benefit from using a struct instead of a class.
Why?
My question is - every time you call a method on a struct, is it boxed?
No
Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Hi Nathan, I am writing an app that might largely benefit from using a struct instead of a class.
There is little or no performance reason to prefer a struct over a class,
little or no efficiency reason, and little or no readability reason.
Structs are useful for making API calls to unmanaged code.
Structs are useful in only the smallest number of cases, and personally,
I've never used them in C# outside of unmanaged API calls. In fact, I've
never even _seen_ them used in the hundreds of thousands of lines of code
that have been delivered in the systems that I've overseen, reviewed, or
participated in.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Nathan Neitzke" <ne******@erau. edu> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
However, it needs to be rich enough where there are methods available.
My question is - every time you call a method on a struct, is it boxed? Because that would be a huge perf hit.
I am assuming that it does have to be boxed, but if anyone knows for sure that would be great!
>There is little or no performance reason to prefer a struct over a
class, little or no efficiency reason
Correct me if I am wrong, but struct/value type in managed code is
mainly to improve performance. If you have large amount of data,
creating it on the stack is considerably less expensive than going on
the heap.
I have personally never used structs in my code (which is not really
saying much) but I have always been looking for some reason which will
justify it based solely from performance point of view. I have always
thought of the sceanrio where a large number of objects are getting
allocated/deallocated to be the driving force behind using a value type
in managed code.
---------
Ajay Kalra aj*******@yahoo .com
"Nick Malik [Microsoft]" <ni*******@hotm ail.nospam.com> wrote in message
news:Ip******** ************@co mcast.com...
.... Structs are useful in only the smallest number of cases, and personally, I've never used them in C# outside of unmanaged API calls. In fact, I've never even _seen_ them used in the hundreds of thousands of lines of code that have been delivered in the systems that I've overseen, reviewed, or participated in.
I can think of cases where using classes instead of structs would make a
semantic difference. In the example below, if Currency was a reference type
the price of the product would be increased too, which is wrong:
lineItem.Price = product.Price;
lineItem.Price += new Currency(20, "CHF");
But as you Nick, I never saw it in real systems...
Alexander
Oops, it should be:
lineItem.Price = product.Price;
lineItem.Price += new Currency(20, "CHF");
Argh...
Something somewhere eats my plus signs!
Anyway it's the increment on the second line, "plus equals".
"Alexander Shirshov" <al*******@omni talented.com> wrote in message
news:eT******** ******@tk2msftn gp13.phx.gbl... Oops, it should be:
lineItem.Price = product.Price; lineItem.Price = new Currency(20, "CHF");
Nick Malik [Microsoft] <ni*******@hotm ail.nospam.com> wrote: I am writing an app that might largely benefit from using a struct instead of a class. There is little or no performance reason to prefer a struct over a class, little or no efficiency reason, and little or no readability reason. Structs are useful for making API calls to unmanaged code.
So you'd be happy if Int32 were a reference type, for example? I have
to disagree - the performance cost there would be dreadful. Imagine:
for (int i=0; i < 100000; i++)
{
....
}
You'd have created 100,000 objects on the heap, just for the iteration!
Structs are useful in only the smallest number of cases, and personally, I've never used them in C# outside of unmanaged API calls. In fact, I've never even _seen_ them used in the hundreds of thousands of lines of code that have been delivered in the systems that I've overseen, reviewed, or participated in.
I agree that they're very rarely useful outside interop, but they do
occasionally have their uses. I have two examples:
1) http://www.pobox.com/~skeet/csharp/t...ernative.shtml
2) Things which are units of measure. I've recently been writing some
code to do with coverage, and it's handy to have a Coverage struct
which is basically two ints ("total sequence points" and "hit sequence
points"). It's logically a value type in the same way that Int32 is,
and it's nice not to have to create a new (separate) object on the heap
every time I create one of them.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nathan,
I find that about boxing and unboxing always very interesting.
I made a simple test, can you try it.
In this test there is by the way 400,000,000 times a boxing or an unboxing
the results are milliseconds
{
int end1=0;
int end2=0;
for (int y = 0; y<1000;y++)
{
int start1 = Environment.Tic kCount;
for (object i=0; (int)i < 100000; i = (int) i + 1)
//This is very inefficient because the increment of the indexer
{
//Generated ILS code
//IL_000d: stloc.3
//IL_000e: ldc.i4.0
//IL_000f: box [mscorlib]System.Int32
//IL_0014: stloc.s i
//IL_0016: br.s IL_0029
//IL_0018: ldloc.s i
//IL_001a: unbox [mscorlib]System.Int32
//IL_001f: ldind.i4
//IL_0020: ldc.i4.1
//IL_0021: add
//IL_0022: box [mscorlib]System.Int32
//IL_0027: stloc.s i
//IL_0029: ldloc.s i
//IL_002b: unbox [mscorlib]System.Int32
//IL_0030: ldind.i4
//IL_0031: ldc.i4 0x186a0
//IL_0036: blt.s IL_0018
//IL_0038: ldloc.0
}
end1 += Environment.Tic kCount-start1;
int start2 = Environment.Tic kCount;
for (int i = 0; i < 100000; i++)
{
//Generated Ils code
//IL_003e: ldloc.3
//IL_003f: sub
//IL_0040: add
//IL_0041: stloc.0
}
end2 += Environment.Tic kCount-start2;
}
MessageBox.Show ("Object: " + end1.ToString()
+ " Integer: " + end2.ToString() );
}
Cor
I occassionaly return an error struct with a bool field.
Regards,
Jeff
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Lora Connors |
last post by:
What is Boxing & UnBoxing in .NET?
|
by: Alistair Welchman |
last post by:
I have a Hashtable of ints keyed on Guids, and I want to do the following:
foreach ( DataRow row in dsWorkDays.Tables.Rows )
{
Guid PersonId = (Guid)row;
DateTime Day = (DateTime)row;
|
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...
|
by: Craig |
last post by:
Hi everyone,
As a relative new-comer to the wonderful world of .NET Framework and
the C# langauge I have come across something that I would like to
clarify (hopefully with the help of kind people such as yourselves).
To quote, "Everything in C# is an object" (including "primitive" value
types, primitive being in quotations because this is what my question
concerns).
|
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...
| |
by: J.Marsch |
last post by:
Hello all:
I am trying to introduce the concept of boxing (and some of the hang-ups) to
some developers that are coming onto a project. A while back, I read a
really cool article that was formatted as a quiz. It introduced a number of
the weird little nuances that you run into when implicit boxing occurs in
your code (like values not being changed when you expected them to -- do to
the fact that a copy was made during the box or unbox...
|
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 you pass a Value-Type to method call
,Boxing and Unboxing would happen,Consider the following snippet:
int a=1355;
myMethod(a);
......
|
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.
|
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
|
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...
|
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...
| |
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...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |