tareqimbasher / NetPad

A cross-platform C# editor and playground.
MIT License
1.25k stars 66 forks source link

Dump not working inside Main Method #89

Closed eslamo closed 11 months ago

eslamo commented 11 months ago

I'm Using Netpad 0.4.2 on MacOS

Output from Console.WriteLine works

but Dump is not working

class Tech
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class Startup
{
    public static void Main()
    {
        var t = new Tech
        {
            Id = 1,
            Name = "Test Name"
        };

        Console.WriteLine("Output");
        t.Dump();
    }
}
tareqimbasher commented 11 months ago

The primary approach is to write Top-Level statements, ie:

var t = new Tech
{
    Id = 1,
    Name = "Test Name"
};

Console.WriteLine("Output");
t.Dump();

class Tech
{
    public int Id { get; set; }
    public string Name { get; set; }
}

If however you still would like to have your code run within a standard Main() entry point rename the class to partial class Program:

partial class Program
{
    static void Main()
    {
        var t = new Tech
        {
            Id = 1,
            Name = "Test Name"
        };

        Console.WriteLine("Output");
        t.Dump();
    }
}

class Tech
{
    public int Id { get; set; }
    public string Name { get; set; }
}

This is because NetPad includes a partial Program class in your script, behind the scenes, to initialize certain runtime services to facilitate interfacing between the process running your script and NetPad itself. Declaring a Main entry point in a different class name, Startup, will make .NET skip the default Program class, also skipping the runtime services initialization.

Another option without the class wrapper is:

void Main(string[] args)
{
    var t = new Tech
    {
        Id = 1,
        Name = "Test Name"
    };

    Console.WriteLine("Output");
    t.Dump();
}

Main(args);

class Tech
{
    public int Id { get; set; }
    public string Name { get; set; }
}
tareqimbasher commented 11 months ago

Closing this issue. Please feel free to reopen it if you face any issues.