Skip to content

System.InvalidProgramException when constructing a record struct with parameter list and calling the default constructor #58328

@V0ldek

Description

@V0ldek

Version Used:
.NET SDK:
Version: 6.0.100
Commit: 9e8b04bbff

Steps to Reproduce:

  1. Create a record struct with a parameter list with at least two parameters:
public record struct S(char First, char Second)
{
}
  1. Add a constructor that takes a parameter of some type (the exact type seems to not matter):
public record struct S(char First, char Second)
{
    public S(object o)
    {
    }
}
  1. This produces CS8862: A constructor declared in a record with parameter list must have 'this' constructor initializer, which is expected. To fix this error, add a call to the default constructor:
public record struct S(char First, char Second)
{
    public S(object o) : this()
    {
    }
}
  1. Attempt to create an instance of the struct:
var s = new S(new object());

Console.WriteLine(s);

Expected Behavior:
The struct is created and printed with First and Second set to default(char).

Actual Behavior:
A System.InvalidProgramException is thrown during execution:

Unhandled exception. System.InvalidProgramException: Common Language Runtime detected an invalid program. 
    at S..ctor(Object o)                                                                                                    
    at Program.<Main>$(String[] args) in D:\<path_ommited>\Program.cs:line 1

Full code:

Occurs both when running compiled to Debug and Release.

var s = new S(new object());

System.Console.WriteLine(s);

public record struct S(char First, char Second)
{
    public S(object o) : this()
    {
    }
}

https://dotnetfiddle.net/85o0Th

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions