2024-05-25 - In IL: Field Declarations
In IL
- Part 1 - Introduction
- Part 2 - Variables and Types
- Part 3 - Variables in Visual Basic .NET
- Part 4 - Instructions and the Stack
- Part 5 - Volume of a Cylinder (Operations)
- Part 6 - Branching Instructions
- Part 7 - Largest of Two Numbers (if-else)
- Part 8 - Largest of Three Numbers (If-ElseIf-Else)
- Part 9 - Switch instruction
- Part 10 - Grade Analyser (switch)
- Part 11 - Prize Calculator (switch-2)
- Part 12 - VB Grade Analyser (Select)
- Part 13 - Loop Instructions
- Part 14 - Print the Alphabet (while)
- Part 15 - Print the Alphabet (do while, for)
- Part 16 - Print the Alphabet (Do Until)
- Part 17 - Print the Alphabet (break, continue)
- Part 18 - Array Instructions
- Part 19 - Summing Arrays
- Part 20 - Other Instructions
- Part 21 - Assemblies
- Part 22 - Class Definitions
- Part 23 - C# Classes and Structs
- Part 24 - VB Classes, Modules and Structures
- Part 25 - Field Definitions
- Part 26 - Field Declarations
Last time we looked at the options for declaring fields in IL, today we are going to look at some examples.
This C# program has a variety of fields defined.
Let's start by looking at the accessibility attributes.
As expected we have the .field directive, the accessibility attribute, the type and the name of the field. The accessibility attributes line up the same as they did for the nested classes that we looked at previously.
Next we have some examples of the contract attributes.
Again this is as we expect. All the fields are private because we omitted the accessibility marker. The const field became static literal to indicate that it is stored with the class and it can only have the value specified. Note that the literal assigned to the field is wrapped in a type specifier. readonly became initonly with both meaning that the field can only be set in a constructor. The static field is also marked as static in IL.
Now let's look at some different type identifiers.
First we have the FileMode field, in IL this is marked valuetype to indicate it's a value type (it's an enum) and then we get the fully qualified name of the type [mscorlib]System.IO.FileMode. The [mscorlib] part indicates the assembly that contains the type. The FileInfo field is marked class to indicate that it's a reference type. The Complex field is also marked as valuetype because it's a struct and we can see it's from a different assembly ([System.Numerics]) than the others. Finally we have the built in types which just replace the C# name for the type with the IL name for the time.
To finish off, let's look at some array fields.
Arrays are indicated by the [] at the end of the type name the same way they are in C#.
Well this was all very straight forward. Next time we will look at the instructions used to work with fields
Comments: