struct and class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jitensandu
    New Member
    • Apr 2010
    • 1

    struct and class

    Please can any one tell me when to use struct and when to use class in c# with example which show advantage of using struct over class.

    and difference between struct and class

    thanks in advance
    jitendra
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Struct is a value type and Class are reference types....

    You can't compare a class and a struct....
    A struct can nowhere be compared to a class...

    A Class has huge and very much confined implementations .... than struct..

    Struct stands nowhere

    Comment

    • Monomachus
      Recognized Expert New Member
      • Apr 2008
      • 127

      #3
      Originally posted by jitensandu
      Please can any one tell me when to use struct and when to use class in c# with example which show advantage of using struct over class.

      and difference between struct and class

      thanks in advance
      jitendra
      Well, structures aren't that bad for really tiny objects.
      Structures are a composite of other types that makes it easier to work with the related data represented by those other types. The simplest example of this is System.Drawing. Point, which contains X and Y integer properties that define the horizontal and vertical coordinates of a point. ...
      While the functionality is similar, structures are usually more efficient than classes. You should define a structure, rather than a class, if the type will perform better as a value type than a reference type. Specifically, structure types should meet all of these criteria:
      • Represents a single value logically.
      • Has an instance size that is less than 16 bytes
      • Is not frequently changed after creation
      • Is not cast to a reference type. (Casting is the process of converting between types.)
      This is extracted from MCTS 70-536.

      So here are the advantages.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        You really should check out this article because it will cover the topic better than we will: Structs Tutorial

        I was going to say that the biggest difference between a struct and a class is that a struct is a Value Type and a class is a Reference Type...but this is covered in the article too!
        Originally posted by MSDN article: Structs Tutorial
        MSDN Structs Tutorial:
        Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well.
        I recommend reading the article :)

        -Frinny

        Comment

        Working...