-7

I have a standard Web API application. Let's assume that in an action I would like to return response like this:

public IHttpActionResult GetSomething()
{
    var model = new Domain.OutcomingModels.Action[1];
    return Ok(model);
}

For model object I could specify to be a class or a struct. From my point of view there are no reasons to prefer struct over class, concerning performance.

The app will handle millions of client's requests per seconds. Things that I'm concerned about:

  • Time to create class over struct
  • Memory consumption on class or struct
  • Time to pass the object to a method that will return IHttpActionResult
  • Memory consumption to pass the object to such method
  • Time on serialization
  • Possible issues with heap defragmentation
  • Possible issues with garbage collector in general

Am I right?

The question is not about general difference between classes and structures. It is about using them with the methods like Ok or BadRequest that will convert C# objects to serialized objects. I'm asking about possible caveats.

cassandrad
  • 3,412
  • 26
  • 50
  • 4
    MSDN has a page about choosing between them https://msdn.microsoft.com/en-us/library/ms229017(v=vs.110).aspx – juharr Jun 21 '16 at 12:41
  • Possible duplicate of [Structs versus classes](http://stackoverflow.com/questions/3942721/structs-versus-classes) – MakePeaceGreatAgain Jun 21 '16 at 12:43
  • @juharr, I know the difference between class and struct, the question is not about it in general, but about possible caveats in one particular case. If there are no hidden things, then just write it in your answer. – cassandrad Jun 21 '16 at 12:43
  • Added exact things that I'm concerned about. – cassandrad Jun 21 '16 at 13:00
  • 2
    @cassandrad your edit just made your question "too broad" with too many possible answers. There is not one choice that is "better" in all of those concerns - classes are better in some respects, structs are better in others. If I were you I would start with everything as classes, identify _specific_ performance and/or memory problems and address those, possibly by changing _some_ classes to structs. – D Stanley Jun 21 '16 at 13:01
  • @cassandrad I'm sorry to say, but it now looks like you've just Googled a load of potential things to consider and added them to your post - and in doing that, essentially removing/replacing most of the context/original body. Your question is now very, very broad and the scope has changed from what the original answers are answering – Geoff James Jun 21 '16 at 13:03
  • @GeoffJames, no, I haven't googled it, just expected that I could avoid these details and be understood. Okay, if community thinks that it is too broad now, you all guys could just flag the question. – cassandrad Jun 21 '16 at 13:04
  • 2
    I think it would be unfair just to "flag" the question. I might suggest that you edit it back to (somewhat) original, and maybe elaborate on some concerns. Lastly - I would kindly point out that being rude and offensive to other members of the community is **not** going to get you anywhere, fast. – Geoff James Jun 21 '16 at 13:06
  • @GeoffJames, why should I edit the question back if it asks now what I wanted to know and previously the question was misunderstood? – cassandrad Jun 21 '16 at 13:08
  • 1
    I think you're missing my point. Read my comment carefully. I'll reiterate: You don't come onto SO and ask a question "can you help me", to which someone offers help and you say "you're rubbish/lazy etc"; and then *still* have the audacity to expect further help! – Geoff James Jun 21 '16 at 13:10
  • @GeoffJames, well, I'm ok with this, thank you for the clarification. – cassandrad Jun 21 '16 at 13:12

1 Answers1

10

The model will be converted to JSON or XML and returned to the client.

Neither JSON nor XML distinguish between class and struct, so it doesn't matter from that perspective whether you choose class or struct. There is very likely no meaningful performance difference either.

There may be benefits from other parts of your system from using struct, but serialization isn't one of them.

Stick with class until you have a compelling reason to change it to struct. Then you eliminate any concerns about mutable structs, passing a reference/copy, value vs reference equality, etc.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • I'm not asking about what is better — JSON or XML. – cassandrad Jun 21 '16 at 12:49
  • 1
    If I'm right in saying, @cassandrad: I don't think @D Stanley is alluding to your actual *choice* of format. Although the answer does quite rightly point out that this bit in your OP is kind of irrelevant. It does give good pointers on using `class` vs `struct` - as you said, you already know about the differences :) – Geoff James Jun 21 '16 at 12:55
  • 2
    @cassandrad I didn't answer which is better - JSON or XML. You asked if `class` or `struct` is better, and from a serialization standpoint it does not matter. – D Stanley Jun 21 '16 at 12:56
  • @cassandrad I clarified by answer to indicate that I was comparing class and struct, not JSON and XML. – D Stanley Jun 21 '16 at 12:59