Skip to content

Controls whether the local variables in C# methods should be zero-initialized.

License

Notifications You must be signed in to change notification settings

josetr/InitLocals

Repository files navigation

InitLocals

# Build Status

Controls whether the local variables in methods are zero-initialized.

Usage

  • Install package InitLocals.Fody.
  • Mark your method/class/module with the InitLocals attribute.

Sample

using System;
using System.Diagnostics;
using System.Linq;

public class Program
{
    public const int Size = 2048;

    static void Main()
    {
        True();
        False();
    }

    [InitLocals(true)]
    static void True()
    {
        UseStack();
        Span<byte> span = stackalloc byte[Size];
        Debug.Assert(span.ToArray().All(n => n == 0));
    }

    [InitLocals(false)]
    static void False()
    {
        UseStack();
        Span<byte> span = stackalloc byte[Size];
        Debug.Assert(span.ToArray().Any(n => n != 0));
    }

    public static void UseStack()
    {
        Span<byte> span = stackalloc byte[Size];
        span.Fill(125);
    }
}

About

Controls whether the local variables in C# methods should be zero-initialized.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages