Wrapping class fields in struct

I've done a bit of research and can't find anything on this and I cannot think of a reason why you'd do this either. At work I've noticed that one of our programmers older code has this pattern fairly often in it. I haven't noticed it in newer code, but it seems odd to me and I don't see a benefit.

Here is an example of what I'm talking about:

public class Order { Fields f; public Order() { // construct } public int OrderNumber { get { return f.OrderNumber; } set { f.OrderNumber = value } } public DateTime OrderDate { get { return f.OrderDate; } set { f.OrderDate = value } } public decimal OrderCost { get { return f.OrderCost; } set { f.OrderCost = value } } struct Fields { public int OrderNumber; public DateTime OrderDate; public decimal TotalCost; } } 

This is a simplified example. The setters in reality typically fire an implementation of NotifyPropertyChanged, etc. I don't see a good reason for doing this versus just putting the fields as members of the class itself. Also worth noting is that in these Fields structs the members are always other value types.

by oscooter via /r/csharp

Leave a Reply