Structures

Structures are designed to be lightweight alternatives to classes.
They use fewer resources (ie less memory)
In earlier versions of Visual Basic, Structures were called Types.
The word Type in .NET has a completely different meaning
Structures are similar to classes in that they can contain constructors, properties, methods, fields, operators, nested types and indexers.
There are however significant differences between them


Difference Between Structures and Classes

Structures do not support Inheritance - derived implicitely from Object
Structures do not support Destructors - no class or structure can derive from it.
Structures have no initialisation - all member field values must be set in your constructor. Default constructor sets all member fields to their default values. (integer = 0).
Classes are reference types, structures are value types.


Structures are somewhat more efficient in their use of memory in arrays; however they can be less efficient when used in collections (ie arrays) ???
Collections expect references and because structures are value types they must be boxed.
This boxing and unboxing is an unnecessary overhead so classes might be more efficient in large collections.
There is no difference in the way you create constructurs and properties in structures and classes
However you are not allowed to create a custom default constructor for a structure.
That is you cannot create a constructor with no parameters for a structure.


Instead the compiler always creates a default constructor for you and that constructor is used to initialise all the member values to their default values.


If you declare a class as a struct then this declares the class as a value type
This is a special type of class that is represented as a value rather than a reference.
You cannot derive classes or other structs from structs
structs are designed to be used as very lightweight simple objects


int, long, float, double are all examples of structs
string is a class



Structures and Classes Fundamental Differences

You cannot initialise members in a structure definition. If you need to provide initialsation for a structure type you have to provide a constructor
Structures cannot have finalisers because they are not grarbaged collected
Inheritance is not applicable to structures, so they can't inherit from anything else and cannot be used as a base class
Structures can implement interfaces



You cannot use a __gc reference type as a member of a struct, because structs are not garbaged collected, a reference member would have to take part in garbage collection



When to use a Structure

Lets suppose you need to store several pieces of information about a person.
Lets say their Name, Department and Location.
You could create separate variables to represent this information but it is then not obvious that the variables are related.
As a way of defining a relationship between the variables you could create a Person structure.


public struct Person 
{
   public string FullName
   public string Department
   public string Location
}



This makes it much easier to work with

Person objPerson; 
objPerson.FullName = "Richard Smith";
objPerson.Department = "Sales";
objPerson.Location = "New York";


A structure is a restricted, lightweight type that when instantiated makes fewer demands on the operating system and on memory that a conventional class does
A structure cannot inherit from a class or be inherited from but a structure can implement an interface.


A structure is a simple user defined type
A structure is a value type
Structures support: access modifiers, constructors, indexers, methods, fields, nested types, opertors, properties.
They cannot be declared protected

public struct uPerson 
{
   public string sName
   public string sAge
   public date dtBorn
}
Type uPerson 
   sName as String
   sAge as String
   dtBorn as Date
End Type




© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

Top

PrevNext