我正在学习C#,教程上说:“当您运行程序时,C#会寻找名为Main的方法。它使用Main方法作为程序的起点。然后执行两个花括号之间的任何代码。“
但在另一个教程网站上,有一段代码:
using System;
namespace RectangleApplication
{
class Rectangle
{
// member variables
double length;
double width;
public void Acceptdetails()
{
length = 4.5;
width = 3.5;
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
Console.ReadLine();
}
}
}
在下面的Main方法中,有其他方法。(我是新手,所以我假设public void acceptdetails、get area和display都是方法)。我的问题是,为什么它不在顶部紧接着命名空间下面呢?我把这个方法放在那里,它也能正常工作,检查了这里的其他帖子,它说可能作者只是想强调其他事情,但我不完全理解为什么。