Skip to content

Primary-constructor conversion creates an invalid field initializer (CS0236) #3979

Description

@sailro

Input code

public struct Pair
{
	public int First;
	public int Second;

	public Pair(int value)
	{
		First = value;
		Second = First + 1;
	}
}

Erroneous output

public struct Pair(int value)
{
	public int First = value;
	public int Second = First + 1;
}

This does not compile:

error CS0236: A field initializer cannot reference the non-static field, method, or property 'Pair.First'

The constructor should remain explicit because the second assignment depends on an instance field initialized by the first assignment.

Details

Product: ICSharpCode.Decompiler / ilspycmd.

Version: master 2efd871.

The input was compiled in Release for net10.0.

I can submit a small fix and regression test.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions