Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When building POCOs or simple DTOs, can I use structs instead of classes?

Tags:

c#

 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; }
    }
like image 618
Shane Avatar asked Sep 27 '10 18:09

Shane


2 Answers

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).

like image 177
Jon Skeet Avatar answered Oct 22 '22 16:10

Jon Skeet


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

like image 27
Curtis White Avatar answered Oct 22 '22 16:10

Curtis White