In JetBrains Rider, when I create a scratch C# file, how can I execute it? Making the Main method public static and adding a namespace seems to be not enough.
using System;
namespace MyNamespace
{
public class Foo
{
public static void Main()
{
Console.WriteLine("hello");
}
}
}
Rider version: JetBrains Rider 2022.3.1
Windows 10
Adding a namespace is actually not even required. What worked for me is the following:
using System;
class Foo
{
public void Main()
{
Console.WriteLine("hello");
}
}
var x = new Foo();
(and it doesn't matter if Rider does not recognise Foo()
as a valid constructor),
x.Main()
will then run your function
You can also find a good description on this page: https://blog.jetbrains.com/dotnet/2017/12/01/c-interactive-rider/
I hope this helped!