Thread: Package methods in struct

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Croatia
    Posts
    16

    Package methods in struct

    Is it common practice to package methods with other elements(string-s, int-s, float-s, array-s...) of struct?

    Than struct acts as a class or is acting similar to class and that have no sense to me? I am new to OOP .

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    In C#, both classes and structs are understood to be used in the same way: containing methods, properties and variables. The technical difference is that all structs are value types while classes are reference types.

    If in doubt, use class. That's easier and less misunderstandings and side effects for the start.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    Croatia
    Posts
    16
    Quote Originally Posted by nvoigt View Post
    In C#, both classes and structs are understood to be used in the same way: containing methods, properties and variables. The technical difference is that all structs are value types while classes are reference types.

    If in doubt, use class. That's easier and less misunderstandings and side effects for the start.
    Thanks

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What might sound odd is that structs are actually more expensive than classes in C#. You should prefer class over struct unless your object does not change or is immutable. You should never use a struct when the struct is not constant. This can lead to some nasty hard to find errors when structs are re-used and yet their contents are not reset.

    Microsoft's own advice follows:

    Do not define a structure unless the type has all of the following characteristics:
    1.It logically represents a single value, similar to primitive types (integer, double, and so on).
    2.It has an instance size smaller than 16 bytes.
    3.It is immutable.
    4.It will not have to be boxed frequently.


    However note that in the XNA SDK and in some portions of .NET they violate these rules.

    http://www.dotnetperls.com/struct
    Last edited by VirtualAce; 10-27-2012 at 09:08 AM.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by VirtualAce View Post
    However note that in the XNA SDK and in some portions of .NET they violate these rules.
    Mostly because that advice list should be tempered. If followed to the letter, it can lead to classes that are performant, when structs could be used. A possible scenario is an hexagon in a game map.

    Code:
    struct Hexagon
    {
        public int X { get; private set; }
        public int Y { get; private set; }
        public TerrainType Terrain { get; private set; } // an enum
        public bool HasSettlement { get; set; }
        public bool HasRoad { get; set; }
    
        public List<Hexagon> GetNeighboors() { ... } 
    
        public override bool Equals(object obj) { ... } 
        public static bool operator ==(Hexagon hex1, Hexagon hex2) { ... } 
        public static bool operator !=(Hexagon hex1, Hexagon hex2) { ... } 
    }
    That struct violates all of the items in Microsoft's advice list. And yet it can have maybe ~62,000 instances at one time (in a 250x250 map) and will almost exclusively be used inside a generic container. probably a List<Hexagon> representing the map.

    If we follow Microsoft advice we will be putting a great load on the garbage collector, which too can greatly affect performance. A concern if that List is going to be recreated numerous times. Probably much better to keep Hexagon as a struct since the generic container will ensure its contents remain in the stack and are thus much more quickly freed from memory. But on the other hand, if that List is going to persist during the whole lifetime of the application, it should be a mistake not to serve those Hexagon instances to the heap and thus change it to a class.

    What I'm saying is that we should avoid going from 8 to 88. From believing that structs were lightweight instances (which was indeed a misconception) to believing structs are more performant (which is also a misconception). We will often get into tough situations where we will not be sure what to do and bootlenecks rarely are were we expect them to be, so the best option is to do some extensive profiling and chose based on the individual requirements of the situation. If there's a tradeoff (almost always there is), make a decision based on what is that exactly we want to preserve; memory or performance.
    Last edited by Mario F.; 10-28-2012 at 04:50 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you inherit Methods into other methods?
    By bobbelPoP in forum C++ Programming
    Replies: 5
    Last Post: 08-02-2008, 08:32 AM
  2. RSA: Do I need to use a big int package for this?
    By MrSteve in forum C Programming
    Replies: 3
    Last Post: 04-27-2008, 06:05 AM
  3. 'portable' package
    By janklojo in forum C++ Programming
    Replies: 2
    Last Post: 12-22-2006, 01:58 PM
  4. C++ usb/serial package?
    By Hankyaku in forum C++ Programming
    Replies: 2
    Last Post: 12-08-2004, 12:39 PM
  5. C++ usb/serial package?
    By revelation437 in forum C++ Programming
    Replies: 5
    Last Post: 03-12-2004, 06:25 PM

Tags for this Thread