13
 public class Customer
    {
        public int CustomerId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public struct Customer
    {
        public int CustomerId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
Shane
  • 4,185
  • 8
  • 47
  • 64
  • PS: I always use classes, but was just wondering if there were any good reasons to use structs...most seem to point to possible performance gains, but that might not be the case. – Shane Sep 27 '10 at 18:34

3 Answers3

14

Your second snippet is a mutable struct. That's a really bad idea, IMO - they behave oddly in various different situations, as values can be copied when you may not expect them to be.

You could create immutable structs of course, and there are times when that's appropriate - but personally I find the reference type behaviour more natural usually. With structs, you also need to worry about the fact that whatever constructors you put in place, it's always possible to set a variable to the default value - so the fields will be zero for numeric types, null for reference types etc. It's annoying to have to deal with the possibility of an invalid object everywhere, whereas with classes you can add appropriate validation to the constructor (or factory methods) to make sure that the only thing you need to worry about is a null reference.

The efficiency argument ends up tricky, as there are pros and cons on both sides, depending on exactly what you do with the objects.

To cut a long answer short (too late?) - I would use classes by default; save value types for things which are natural individual values ("an instant in time", or "an integer" for example).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    So sounds like there are rare cases when I should use a struct. I know that there is no struct in Java, but C# chose to include them. – Shane Sep 27 '10 at 18:34
  • 1
    @Shane: Yes - it's pretty rare for most projects, in my experience. But it's reasonable for types such as Guid, TimeSpan and DateTime to be value types. – Jon Skeet Sep 27 '10 at 18:43
1

Here is a link with differences between structs and classes:

http://msdn.microsoft.com/en-us/library/aa288471%28VS.71%29.aspx

It claims structs are more efficient but I would think that pass by ref is more efficient then pass by value for large data.

This may be a problem:

" Although the CLR allows it, C# does not allow structs to have a default parameterless constructor. The reason is that, for a value type, compilers by default neither generate a default constructor, nor do they generate a call to the default constructor. So, even if you happened to define a default constructor, it will not be called and that will only confuse you. To avoid such problems, the C# compiler disallows definition of a default constructor by the user. And because it doesn't generate a default constructor, you can't initialize fields when defining them, like: Collapse

struct MyWrongFoo { int x = 1; }

Remember, the compiler puts all this initialization code into the constructor (every constructor), and because there's no default constructor, you can't do the initialization.

Now, for the fun part.. You normally instantiate a struct like this:"

http://www.codeproject.com/KB/cs/structs_in_csharp.aspx

Curtis White
  • 6,213
  • 12
  • 59
  • 83
  • Pass by reference and pass by value don't quite mean what you think they do, I suspect. See http://pobox.com/~skeet/csharp/parameters.html – Jon Skeet Sep 27 '10 at 18:25
  • @Jon: Thanks for link. Yes, I went over that a couple years back in great detail. Looks like it is time for me to go back. ;0 – Curtis White Sep 27 '10 at 18:27
1

POCOs and DTOs are not the same thing.

A POCO is a business object that would typically have state and behaviour.

A DTO is a lightweight object for transferring state between application layers.

I would obviously use classes for POCOs, and also for DTOs.

Joe Ratzer
  • 18,176
  • 3
  • 37
  • 51
  • so when would I ever use a struct? I don't think I ever see them used anymore. Are they obsolete? – Shane Sep 27 '10 at 18:30
  • They are not obsolete - the language is full of them! Creating your own is a bit different and I rarely find the need for them. This is a good conversation on when to use them - http://stackoverflow.com/questions/521298/when-to-use-struct-in-c – Joe Ratzer Sep 27 '10 at 18:35