1

As I'm learning c# I would appreciate some professional advice about structs and classes. As classes are reference types and stored on the heap, while structs are value types and stored on the stack.

What I understand is that structs are typically used for smaller data types for performance reasons.

Given with what I have read, would the following code be a struct or a class.

public struct DisplayWeatherAstronomy { public string SunRise { get; internal set; } public string SunSet { get; internal set; } public string MoonRise { get; internal set; } public string MoonSet { get; internal set; } } 

/***Extra code***/

Going off two replies, I've added some extra code.

public IEnumerable<DisplayWeatherAstronomy> WeatherAstronomy(string id) { var doc = WeatherXml.XmlData(id); var displayAstronomy = (from wd in doc.Descendants("astronomy") select new DisplayWeatherAstronomy { SunRise = (string)wd.Element("sunrise") ?? string.Empty, SunSet = (string)wd.Element("sunset") ?? string.Empty }); return displayAstronomy; } 
7
  • Relevant: blogs.msdn.com/b/ericlippert/archive/2009/04/27/… and blogs.msdn.com/b/ericlippert/archive/2009/05/04/… Commented Jun 15, 2013 at 12:41
  • this could be easily a struct simply because i dont see any methods to change the state of the members.you dont need the overhead of a reference type if its this simple. Commented Jun 15, 2013 at 12:45
  • Thanks for the reply, I have added some extra code to try and help me understand Commented Jun 15, 2013 at 13:09
  • possible duplicate of C# - Garbage Collection Commented Jun 15, 2013 at 15:34
  • @JMK That doesn't sound like the same question to me at all. Commented Jun 15, 2013 at 17:45

3 Answers 3

4

This depends more on the purpose of your type than on the size.

If you want to pass data around and want a copy for each call you should use a struct. But if you need to have your data stored at a central location and many other parts of your code should work on the same data, then you need a class. A class is passed by reference and does not duplicate any data.

If you copy data around, as structs do, you can end up with an inconsistent state.

Sign up to request clarification or add additional context in comments.

5 Comments

Actually, passing a class around is usually faster than passing a struct, because a reference is almost always smaller than the whole struct. What's faster is accessing the data in a struct, because there is one less indirection (and no null checking).
Hi Thalm, thanks for the reply, I'm going to re-read the chapter, as I think I may have misunderstood it.
@svick you are right, its not so much about speed, but more about the correct use of the type.
@GeorgePhillipson i also vote for my answer as the correct one, because what hans mentioned is specific to your code and does not consider the different use cases of value types vs. reference types. as its explained in detail here: blogs.msdn.com/b/ericlippert/archive/2009/04/27/…
Hi Thalm thanks for the reply, both are good answers, but I can only mark one as correct, that is why I also clicked on the up arrow for your reply, if I could have marked both as correct I would have.
2

First thing you want to do is get your data structure defined correctly. That's an issue with the one you have, it stores times as strings. That's not a great type selection, it is both expensive since you'll need to convert them from the string representation to a time and it painful to make your program work in different locales in the world that have different ways to express a time of day. You definitely want a type that can express a time unambiguously and quickly. A TimeSpan is the correct type for storing a time of the day. It is directly compatible with the DateTime.TimeOfDay property and is stored as a binary value, 64-bits (8 bytes).

Next guidance is that a struct needs to be light-weight to be efficient at runtime. The rule of thumb is that it should not have more than 4 fields and not be larger than 16 bytes. If you exceed those limits then a value type loses its speed advantage. Four fields is good on an x86 processor, it has 4 spare cpu registers than can store values. 16 bytes sets an upper limit on the cost of copying a value when it is passed by value instead of reference.

You're good on the number of fields but exceed the size limit, 4 TimeSpans take 32 bytes. So this should be a class.

3 Comments

Hi Hans thanks for the reply, the reason I chose a string was because the data was in a string format from the remote site. So if I understand you correctly, no more than 4 fields and less than 16 bytes. (Not mentioned in book i'm reading), will be ok for structs, anything bigger will be a class.
You are going to have to convert those strings sooner or later. Sooner is always better. Not in the least because only the code that talks to the web service would have a decent shot at knowing how the string is encoded.
Hi Hans true, hopefully going to college in September for HND in computing (Adult Student), books can only teach you so much. I think I learn more reading info on sites like this and posting questions than I do reading books.
0

As classes are reference types and stored on the heap, while structs are value types and stored on the stack.

Wrong.

Given with what I have read, would the following code be a struct or a class.

That would be a bad struct, because it's mutable (even if the setters are internal). In general, at least 95 % of the time you want to use a class. You should create a custom struct when your profiling shows that using a class is slow, and that happens very rarely.

1 Comment

Hi svick, thanks for the reply, I will go over chapter again as I may have misread it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.