
What is the stsfld instruction in the .NET Framework intermediate language? This instruction stores the value on top of the evaluation stack into a static field, as we demonstrate through this example program.
This .NET IL example shows the stsfld instruction. It provides the source C# program.

So, let's get started with this example C# program. We assign the static field _a in the Program type to the value 3. In the intermediate language, we load a constant value 3 (ldc.i4.3) onto the top of the evaluation stack. Then, we have a stsfld with the argument being the address of the field _a. So the top value (3) is stored into the location pointed to by _a.
Program that has stsfld instruction [C#]
using System;
class Program
{
static int _a;
static void Main()
{
Program._a = 3;
if (Program._a == 4)
{
Console.WriteLine(true);
}
}
}
IL
.method private hidebysig static void Main() cil managed
{
.entrypoint
// Code size 21 (0x15)
.maxstack 8
IL_0000: ldc.i4.3
IL_0001: stsfld int32 Program::_a
IL_0006: ldsfld int32 Program::_a
IL_000b: ldc.i4.4
IL_000c: bne.un.s IL_0014
IL_000e: ldc.i4.1
IL_000f: call void [mscorlib]System.Console::WriteLine(bool)
IL_0014: ret
} // end of method Program::Main
What interesting things can we learn about static versus instance fields this way? I have found that static fields are somewhat faster to use than instance fields; for performance, then, you can try to use mostly static fields. Also, avoiding stsfld instructions is probably the best tip: if you can do most computations in local variables, you can get even better performance. Stsfld is, unfortunately, a bit of a slowdown.
Local Variable Field Optimization Static FieldWe looked at the stsfld instruction in the intermediate language. This instruction is used for static fields in the C# language and Shared fields in the VB.NET language. It is common and therefore useful to know something about. Micro-optimizations are not usually useful though.
Intermediate Language