---
title: Structure
slug: structure
createdAt: 2021-06-23T18:31:54.000Z
---

A `structure` is a <u>mutable</u> datatype that contains a collection of named fields.

This is very similiar to a POCO class in C#

The fields of a structure can be other structures, records, and services.

A structure is a Type.  You can declare a new instance of it and such can have local defined constructors - [Constructor](docId:8cur6rtDH4zhi8fdH6VhO)

## Declaration

To declare a new structure define a top level construct using the keyword `structure` followed by the desired Identifier. The name must be unique within the corresponding namespace - [Namespace](docId:_Gwa7zz7FnLe1mVM4rbHN)

A structure contains any number of named Fields - [Field](docId\:sZ10Ih-owPhMJGSwgtAVc) They are mutable and the structure's fields can be assigned new values at runtime. They can also be initialized to initial values.

:::CodeblockTabs
Shift

```csharp
 //Physically located in Structures
structure AnotherStructure
{
    int Pa;

    this(int pa)
    {
        Pa = pa;
    }

}

structure ExampleStructure
{
    string Foo;
    string Bar = "Why hello there";
    AnotherStructure Baz;
    AnotherStructure Baz2 = new AnotherStructure(5);//Constructors are supported
    AnotherStructure Baz3 = new AnotherStructure();
    AnotherStructure Baz4 = new AnotherStructure { Pa = 3 };//Initializer syntax is supported
}
```
:::

### Remarks

Shift structures have nothing to do with C#'s structs. They are pass by reference and live on the heap.

There are no properties in Shift, with no getter and setters for behavior.

All structures have a system supplied 0 parameter constructor if not defined by the user. C# only supplies this if the user supplies no constructors. Supplying a constructor does not block this in Shift.

**C# Binding**

In general any C# class without behavior will be bound to a structure in Shift. C# structs that have mutability will be also bound as a structure.

### C# Equivalent

All fields in a Shift structure are equivilant to a property in C#.

```csharp
using System;

namespace ExampleApp.Structures
{

    public class AnotherStructure
    {
        public int Pa { get; set; } = new int();

        public AnotherStructure(int pa)
        {
            Pa = pa;
        }

    }

    public class ExampleStructure()
    {
        public string Foo { get; set; } = string.Empty;
        public string Bar { get; set; } = "Why hello there";
        public AnotherStructure Baz { get; set; } = new AnotherStructure();
        public AnotherStructure Baz2 { get; set; } = new AnotherStructure(5);
        public AnotherStructure Baz3 { get; set; } = new AnotherStructure();
        public AnotherStructure Baz4 { get; set; } = new AnotherStructure { Pa = 3 };
    }

}
```

Most of the interesting implications of a structure come from how Types and constructors work in Shift. Take this C# class.

```csharp
class Bar
{
    public int Honk { get; set; } = 7;
}

class Foo
{
    //All fields are private are unusuable
    //The lack of the constructor basically makes this class pretty useless without
    //reflection cleverness; which is its own can of worms

    int A { get; set; }// Is a structure that defaults to 0
    String B { get; set; }// Is a class that defaults to null
    Bar C { get; set; }// Is a class that defaults to null
    AnotherStructure D { get; set; }// Is a class that defaults to null
}
```

Now compare to a similiar datatype in Shift

```csharp
structure Bar
{
    int Honk = 7;
}

structure Foo
{
    int A;// 0
    String B;// ""
    Bar C;// new Bar()
    AnotherStructure D;// new AnotherStructure
}

//All fields are accessible through an instance of the structure
```

Check out the field article for a deeper dive on their declaration and behavior

## Usage

All of a structure's fields can be accessed when you have an instance of a type. Structures are a mutable datatype so their fields can be assigned a new value at runtime.

```csharp
var foo = new ExampleStructure();
Console.WriteLine(foo);// Value<ExampleStructure,T:foo>
Console.WriteLine(foo.Bar);// "Why hello there"
foo.Bar = "Some new value";
Console.WriteLine(foo.Bar);// "Some new value"
```



