User FAQs

If you have a question, please send it to us.


1) What is a Structure ?
A structure is the definition of an object, similar to a data record.
A structure cannot be inherited from.
A structure is a value data type (and therefore should not contain any reference data type members).
A structure can contain implementation.
A structure cannot be declared static although it can have static members.
Structures can be 'public', 'private', 'internal'.
The default access modifier for a structure is 'internal'.

internal struct MyStructure 
{
   public int _NumberField;
   internal void MyMethod();
   private bool Property_Name { get; set: }
}

A structure can contain fields, methods, properties, enumerations, delegates, events, constructors, indexers, operator methods, classes.
A structure cannot contain finalizers or destructors.


2) What type of Access Modifiers can you have on Structure members ?
Structure members can be 'public', 'private', 'internal'.
Structure members cannot be 'protected internal' or 'protected' because they do not support inheritance.
The default access modifier for a structure member is 'private'.


3) What is the difference between a Structure and a Class ?
Structures are value types (stored on the stack), but classes are reference types (stored on the heap).
Structures do not support inheritance because you cannot have 'protected' or 'protected internal' access modifiers.
Structures cannot have destructors, but classes can.
Structures cannot initialise non static fields, but classes can.


4) What are the similarities between a Structure and a Class ?
Both can contain fields, methods, properties.
Both can inherit from interfaces.
Both can have 'public', 'private' and 'internal' access modifiers.
Both can contain implementation.


5) Can a Structure inherit from an Interface ?
Yes. Although the correct term is implement rather than inherit.
A structure can implement one or more interfaces.
A structure cannot inherit from a Structure or Class.

struct MyStructure : IMyInterface, IMyInterface2 
{ }

6) Can a Structure contain a Class ?
Yes. Although this is not best practice.

struct MyStructure 
{
    public class MyClass
    {}
}

7) Can you have nested Structures ?
Yes. It is possible to declare a structure inside another structure.

struct MyStructure 
{
    public struct MyNestedStructure
    {}
}

8) Can a Structure contain a member variable that has a Structure data type.
Yes. Structures can contain member variables that have structure data types.

struct MyStructure_A 
{}

struct MyStructure_B
{
    private MyStructure_A myRecord;
}

9) Can a Structure contain 'static' fields ?
Yes. Structures can contain static fields.
Static fields can be initialised inside a structure.

struct MyStructure 
{
    static string textField;
}

10) Can a Structure contain 'static' methods ?
Yes. Structures can contain static methods.
They can be invoked by using the name of the structure.
Static members can only access other static members.


11) Describe the difference between passing a Structure to a method and passing a Class to a method ?
When a structure is passed by value "a copy" is passed in and the original cannot be changed.
When a class is passed by value "a copy of the address" is passed in allowing the original to be changed.
When a structure (or any other value type) is passed by reference the address is passed in allowing the original to be changed.


12) Can a Structure have a Constructor ?
Yes. Although it will not be the default constructor.
It is possible to include a constructor but it must have at least one parameter.
Any private members can only be initialized in a constructor.
A default constructor (with no parameters) is created automatically and therefore one cannot be defined.
It is an error to initialize an instance field in a structure constructor.

internal struct MyStructure 
{
   private long _privateField;
   public float _numberField;

   public MyStructure(string textValue)
   {
      _privateField = 100;
      _numberField = 10.5;
   }
}

13) Write code to instantiate an instance of a Structure ?
You can create an instance of a Structure using the 'new' keyword.
The first line uses the default parameterless constructor.
The second line uses a custom constructor.

MyStructure struct1 = new MyStructure(); 
MyStructure struct2 = new MyStructure(10,20);

You can also create an instance by just declaring it (without using new).
The fields will remain unassigned and the object cannot be used until all the fields are initialised.

MyStructure struct3; 
struct3.Field1 = 10;
struct3.Field2 = 20;



© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext