Differences between struct and class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • puzzlecracker

    Differences between struct and class

    what are the most usable, prominent, and must-to-know ones?
  • Peter Duniho

    #2
    Re: Differences between struct and class

    On Tue, 07 Oct 2008 20:04:03 -0700, puzzlecracker <ironsel2000@gm ail.com>
    wrote:
    what are the most usable, prominent, and must-to-know ones?

    Comment

    • John

      #3
      Re: Differences between struct and class

      puzzlecracker wrote:
      what are the most usable, prominent, and must-to-know ones?
      From a previous post you seem to be thinking of the C# type system in
      terms of C++. You cannot do this, often the primary differences between
      languages are their types systems. For instance C uses a "tag" system,
      C++ uses a "type" system where everything is a "type" i.e. a class (well
      except fundamental types, anyways, we're not talking C++).

      In C# there is are two fundamental types Value types (structs) and
      Reference types (classes). This effects, primarily their equality and
      assignment semantics, and thus the way they are passed as parameters to
      methods. These semantics permeate everything, it is not like C++ where
      parameters are passed by value by default but can be pass by reference
      using the reference operator (&). There is no such construct on C#,
      value types are value types and reference types are reference types,
      forever. Well not quite, you can "box" a Value type into a Reference
      type, something that should generally be avoided.

      Comment

      • Pavel Minaev

        #4
        Re: Differences between struct and class

        On Oct 8, 7:04 am, puzzlecracker <ironsel2...@gm ail.comwrote:
        what are the most usable, prominent, and must-to-know ones?
        This is a FAQ. Here's a Google Groups link to the last (as far as I
        remember) discussion on the topic - it has my long post, and I do not
        want to retype it :)

        "Why are some types implemented as struct?":

        Comment

        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

          #5
          Re: Differences between struct and class

          puzzlecracker wrote:
          what are the most usable, prominent, and must-to-know ones?
          The most fundamental is that a class is a reference type and a struct is
          a value type.

          To work well, a struct should be small and immutable. The members of a
          struct are generally value types (with a possible exception for strings,
          as they are also immutable).

          If you don't have any special reason to use a struct, stick to creating
          classes.

          --
          Göran Andersson
          _____
          Göran Anderssons privata hemsida.

          Comment

          Working...