Creating Understanding and Educating Programmers, Developers and Technical Communities, one post at a time.

Friday, May 15, 2009

Structure

Structure is a lightweight class in C#. The following list details the differences between structures and classes.

Structure
  • implicity inherits from System.ValueType (always reside on stack)
  • are sealed and can not be inherited, and also cannot inherit classes and other structures
  • The default constructor of a structure cannot be replaced by a custom constructor.
  • Custom constructors of a structure must fully initialize the value of the structure.
  • Dont have a destructor
  • Field initialization is not allowed. Const members of a structure can be initialized.

2 comments:

  1. You should probably be a little more explicit on your third point. A struct is a value-type with value-type semantics - this is hugely important and should probably be your first point.

    ReplyDelete
  2. @Bog Saggett: Now, I edited my post. I placed third point to first position, and also combine previous first and second point into one single second point.

    ReplyDelete