views:

153

answers:

6

C# question.
Say I have a customers class that has a bunch of props for storing string only data such as name postal data and phone numbers.

I don't use this entity for ORM as I'm only adding it to some type of collection for use during app life cycle. Additionally I don't need to add any entity specific methods to it or persist the data to xml or database, or even to a webservice.

Is it better to make this a struct rather than a class? or no benefit? Also side question, should I make the collection Customers, a list structure?

Be harsh, please critique.. :)

 struct customer
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

    }

struct Customers<List>
{
    private customer cust;

    public customer Cust
    {
        get { return cust; }
        set { cust = value; }
    }

}
+1  A: 

Unless you have identified specific reasons for using a struct, use a class.

Update: due to @Dmitry Lobanov: Eric Lippert's post: The Truth About Value Types

Structs vs. Classes

Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well.

Heap or Stack?

When you call the New operator on a class, it will be allocated on the heap. However, when you instantiate a struct, it can be created on the stack. This will yield performance gains. Also, you will not be dealing with references to an instance of a struct as you would with classes. You will be working directly with the struct instance. Because of this, when passing a struct to a method, it's passed by value instead of as a reference.

Ref.

Mitch Wheat
well i guess that's what my question is asking Mitch, have i identified a specific reason (such as performance) for using a struct? Since I'm not going to reuse the entities that are storing the customer data, using a struct shouldn't be ruled out. Just because its a "Customer" type given my constraints why shouldn't I use a struct?
Anonymous Type
Well, I think the things are bit more complicated, here's a great post about value types misunderstanding: http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx
Dmitry Lobanov
+3  A: 

I suppose you could look at When to use struct in C#?

Dynami Le Savard
yeah thanks for the link, if you read the constraints of the question I'm only using it for storing some string data during application execution, so why do any of the points in the question you linked refute use of a struct?
Anonymous Type
+1  A: 

MSDN article (Classes and Structs)

bluevoodoo1
I'm sure some pplz reading this question will find that link quite helpful.
Anonymous Type
+4  A: 

I can't see any value in making the customer a struct. The string fields will all be reference types, so you might as well make the whole thing a reference type (ie. class).

I'd be inclined to use one of the built-in collection types rather than create my on type for Customers. Something like:

List<Customer> Customers = new List<Customer>();
Andrew Cooper
thanks, this is the type of answer I was hoping for cheers mate. btw do you think other than wrapping a whole bunch of reference types in a value type, there would be a small performance gain when building a large dataset of "customers"? i know I should test just wondering.
Anonymous Type
I think you'd have to test the performance. It probably depends a lot on how many fields you're talking about, and what you're doing with them. You won't get much gain in performance during object creation because memory has to be allocated for the strings anyway.
Andrew Cooper
@Anonymous: Also, don't forget that though the garbage collection cost of a large struct full of reference types can be a *tiny* amount faster than that of a class full of reference types, the cost *every single time you copy it* is *much larger*. Value types are not free; they are copied by value, and that can be very slow compared to a reference copy. Entire nanoseconds slower!
Eric Lippert
ahh excellent point Eric, thankyou this is exactly the kind of detail that makes SO such a great place to ask questions.
Anonymous Type
A: 

Be aware of the mis leading fact that

Value type is more faster rather than reference types.

Actually , think about refernces when you pass reference type , you are passing a pointer to an

existing object while passing value type , you are actually passing a whole new object with identical value.

saurabh
+1  A: 

For what it's worth, I would never use structs because I so very rarely have a need to provide a data only structure that doesn't have some sort of associated behaviour (validators, formatters, etc...).

The one thing that I do like about the basic concept of a "struct" is that it represents a storage system at it's most basic, and therefore should avoid the need to write all those pernickity custom getters and setters and all that sort of stuff... but then again, we now have those lovely auto-properties which effectively achieve the same result from purely a coding perspective, and while the YAGNI nazi in me might say to use the struct because it is meant to be simple, the realist in me knows that I will inevitably want to change the struct to a class anyway, so why not simply implement the class from the beginning and be done with the matter! ;-)

As for the argument of performance, and other benefits... ask yourself the question "does this really matter". If you're writing a serious real-time system... perhaps you want to be using another tool. If you're simply passing around some data, you've likely got oodles of processing croutons at your disposal, and your killer algorithm might not really need to worry about the nano-second difference it's going to make.

S.Robins
very well thought out response, thanks mate.
Anonymous Type