Skip to content

Generate class codefix produces invalid code for top-level programs #49649

@Youssef1313

Description

@Youssef1313

Version Used:

master

Steps to Reproduce:

using System;

var student = new Student("Youssef"); // Apply the codefix
Console.WriteLine(student.Name);

Expected Behavior:

using System;

var student = new Student("Youssef");
Console.WriteLine(student.Name);

internal class Student
{
    private string v;

    public Student(string v)
    {
        this.v = v;
    }
}

Actual Behavior:

using System;

var student = new Student("Youssef");

internal class Student
{
    private string v;

    public Student(string v)
    {
        this.v = v;
    }
}

Console.WriteLine(student.Name);

error CS8803: Top-level statements must precede namespace and type declarations.

Note that the class declaration must come after all top-level statements, but the codefix currently puts it in between statements.

This might not be specific to this codefix, but an issue in the code generation process that's used by more than one analyzer

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEBugFeature - Simple ProgramsTop-level statementshelp wantedThe issue is "up for grabs" - add a comment if you are interested in working on it

    Type

    No type

    Projects

    Status

    Completed

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions