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

Class or struct

Hi there

I know this probably have been addressed before, but I have an object that
should contain the following
Guid id
String fName
String lName

Could I use a struct for that? I have read that a struct should only contain
valuetypes and should be below 16byte in memory size. The guid is actually
16 byte in size but can i use string (which are ref-types) in my struct?

The problem is that I have an array of customers that I need to transfer in
a WebService. I could of course create a two-dimentional array or use a
class, but what is the best practice or choice for that matter?

Thanks in advance...
Apr 11 '06 #1
7 1702
Hi Kenneth,

This article outlines some of the differences between C# classes and
structs:
http://www.jaggersoft.com/pubs/StructsVsClasses.htm

HTH,
Chris

Apr 11 '06 #2
Chris' link was very interesting (and has been saved -- there was some
good stuff in there!), but let me boil it down for you:

Structs are value types. They're instantiated on the stack. Classes are
reference types. They're instantiated on the heap. The bottom line is
that you should use a struct only when you *want* value type semantics.
If you pass the struct to a method for whatever reason, do you want the
original struct given to the method, or do you want a copy? If you use
a struct, you are passing a copy of the struct to the method, rather
than a reference to the original struct to the method. If that's what
you want, use a struct. The reason structs should be limited in size is
because of that copying behavior.

To answer your specific question, yes, you can use strings in a struct,
and no, it's not that big a deal. Strings are reference types, yes, but
the upshot of that that is that if you use a string in a struct you are
only adding 4 bytes to the size of your struct. The actual memory for
the string is stored in the heap; you're just storing the reference in
the struct. So the struct you gave as an example would be 24 bytes
large when stored in memory on the stack. Not *too* big, so it wouldn't
be a major performance hit as a struct if you're doing a lot of method
calls.

As for transferring to the web wervice... if it's a .NET web service,
you can easily serialize either a class or a struct, and passing it to
the web service will be roughly the same in terms of size. The choice
of struct or class in that case is basically irrelevant.

Apr 11 '06 #3
ra*******@gmail.com wrote:
Chris' link was very interesting (and has been saved -- there was some
good stuff in there!), but let me boil it down for you:

Structs are value types. They're instantiated on the stack.


That's not quite true. The data lives wherever the variable it's held
by lives (if it's in a variable). For instance, a value type variable
in a reference type will still live on the heap.

See http://www.pobox.com/~skeet/csharp/memory.html for more about this.

Jon

Apr 11 '06 #4
That's a good point, and shame on me for not being more specific. I
meant that when a struct is instantiated within code, i.e. within a
method, it's instantiated on the stack, just like other value types.

E.g.

struct foo
{
int bar;
}

class example
{
public static void Main()
{
foo x = new foo(); // the foo struct named "x" is instantiated on
the stack.
}
}

Apr 11 '06 #5
Hello Chris,

CF> This article outlines some of the differences between C# classes and
CF> structs:
CF> http://www.jaggersoft.com/pubs/StructsVsClasses.htm

One note that was missed in this article is that u can be careful with returning
structs from the method/property.
C# doesn't support returning by ref and we get copy of the structure, not
the initial stuct

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Apr 11 '06 #6
One of my rules of thumb is that as soon as you see some sort of UID
involved, you need a class. Here is my reasoning.

If something has a UID, then you typically want to refer to a single
copy of it, and make a duplicate only upon demand, because you
logically think of "that customer there" as opposed to "this customer
here". You may choose to clone it in order to create multiple versions
(the customer as it is in the database versus the changed customer to
be written to the database), but this is a conscious decision...
something that you do explicitly as part of your system design.

If you designed your system so that an item with a UID were copied
everywhere, I think that you (and other programmers coming after you)
would find the design difficult to follow: constantly having to
remember that there may be dozens or hundreds of copies of this
customer floating around in memory.
From the other side, things with value semantics usually have no unique

identifier. An "int" has no UID... it could serve as a UID for some
other type, but it itself does not have one. That's why copying it on
every call and every method return is not a problem: because there is
no need to distinguish between "this number 5" and "that number 5". The
value is all that matters.

The same goes for Point and Rectangle, by the way, two popular value
types in the .NET Framework: there is no concept of "this Point" versus
"that Point" if the two have the same coordinates.

So, in your case, I would definitely not make your customer a struct. I
would definitely make it a class, even if it were smaller than 16 bytes.

Apr 11 '06 #7

<ra*******@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Chris' link was very interesting (and has been saved -- there was some
good stuff in there!), but let me boil it down for you:
[cut]
To answer your specific question, yes, you can use strings in a struct,
and no, it's not that big a deal. Strings are reference types, yes, but
the upshot of that that is that if you use a string in a struct you are
only adding 4 bytes to the size of your struct. The actual memory for
the string is stored in the heap; you're just storing the reference in
the struct. So the struct you gave as an example would be 24 bytes
large when stored in memory on the stack. Not *too* big, so it wouldn't
be a major performance hit as a struct if you're doing a lot of method
calls.


Note that the absense of a user defined default destructor pretty much
forces you to use to put extra code in your and properties to access
reference types if you don't want unexpected nulls:

struct S
{
private string name;
// public S() { name = "Dilbert"; } // Cannot do this!
public S(string name) {this.name = name;}
public string Name { get { return name == null ? "Dilbert" : name; }}
}
Apr 12 '06 #8

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

Similar topics

2
by: Simon G Best | last post by:
Hello! I have a query regarding explicit specialisation of class templates which are themselves members of class templates. Here's what I want to do: template< class T > struct pink { ...
2
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
4
by: Steve | last post by:
I'll be the first to admit, I'm not entirely clear on the appropriate usage of either. From what I am reading in my books, a Struct and a Class are pretty much the same, with the difference being,...
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
1
by: Jon Slaughter | last post by:
I've managed to put together a template class that basicaly creates a recursive tree that lets you easily specify the "base" class of that tree and and ending notes and lets you stop the recursive...
15
by: bugzilla | last post by:
hi,all, I have a C++ program need to convert to c language to be used in a emabedded system. the problem is that the original code was writtern in C++ language with Parent class and some child...
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
1
by: stromhau | last post by:
Hi, I have made a few classes in c++. They somehow cooperate doing some 3d stuff. Basically it is a moving camera acting as a flight, i have placed a lot of objects around the scene together with...
2
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the...
12
by: Angus | last post by:
I am writing a class as a wrapper around a std::map. The class is: template<class TKey, class TValue> class CGeneralMap : protected map<TKey, TValue> At the moment I have basic functions like...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.