开发者

Is there any issue by using this struct?

I have read a lot about the danger of using structures as a data type, I wonder if there is any problem with this one,

List<Summarized> SummarizedList = new List<Summarized>();
    Summarized SumInfo;
    struct Summarized
    {
        public string sBrand;
        public string sModel;
        public string sCustomer;
        public int sline;
        public string sLeader;            
        public int sDesire;
        public int sReal;               
    } 

As you can see I use a generic list of items type SumInfo which is the struct data type. whenever I need to update an item of the list I just do the following:

SumInfo = (Summarized)SummarizedList[CurrentPos];
      开发者_如何学Python      SumInfo.sDesire = DesireProd;
            SumInfo.sReal = RealProduced;                
            SummarizedList[CurrentPos] = SumInfo;

where CurrentPos is the position of the item I want to update.

Everything works fine so far, so could be any problem with that in the future? Is this struct one of the mutable ones?

Thanks.


Everything works fine so far, so could be any problem with that in the future? Is this struct one of the mutable ones?

Yes, it's a mutable struct. Yes, it will work.

However, I would argue that this struct should be a class. For details as to why, refer to Choosing Between Classes and Structures. In this case, you're violating 3 of the cases which should always be true for structs:

  • It logically represents a single value, similar to primitive types (integer, double, and so on).
  • It has an instance size smaller than 16 bytes.
  • It is immutable.

This would strongly suggest a class is more appropriate for your type. It would also simplify your code.


BTW - Side note, the cast is not required in your code above. You should be able to just write:

SumInfo = SummarizedList[CurrentPos];


Issues:

  • It's a mutable struct, and they're almost always a bad idea (search for "mutable structs evil" and you'll get loads of hits)
  • It's got public fields - therefore no encapsulation; no separation between the API of the type an its implementation
  • It's got public members which don't follow the normal .NET naming conventions
  • It doesn't logically represent a single value, as per the .NET design guidelines
  • It's larger than the 16 bytes recommended by the same guidelines (although I wouldn't pay too much attention to that if everything else were okay)

Basically it's a dumb data bucket. There's a time and place for that, but it should almost always be a class in that case, and I'd personally usually try to make it an immutable type as well.


is there any reason you're using a struct? if you made it a class, the List would just contain references, and your code would look like:

SumInfo = SummarizedList[CurrentPos];
SumInfo.sDesire = DesireProd;
SumInfo.sReal = RealProduced;  
// you're done! no need to insert it back in, you're referring to the same item


Personally, I would have nothing against using this struct. It may depend more on how you use it, whether you encapsulate the List methods etc.

The mutability of it depends on whether you are expecting to update any antries once you have added them to the list. If you are not expecting to, then your STRUCT is immutable, but your list isn't. However in this case you are updating the entries, so it is.

I would concur that a class is probably a better option for this.


Issue 1:

Yes the struct is mutable. And it suffers from all the problems associated with that.

SummarizedList[CurrentPos].sDesire=DesireProd;

shows why using a mutable struct like this is a bad idea, since it will not change the struct in the list.

Issue 2:

You have public mutable fields. This is considered bad style and you should use properties instead.

Issue 3:

You're using (System) Hungarian notation. When developing in C# follow the .net naming conventions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜