C#实用工具类 - 设置一般使用和编译没有主要

Jon*_*itt 1 c# utility-method

好的,所以努力学习一些C#.伟大的语言,喜欢与它一起工作,但我不理解如何克服缺乏实用程序类.本质上,我想设置一个通用实用程序类,它可以包含在一个文件夹中,只需通过"使用命名空间Globals/Utilities/etc"就可以用于任何项目.命令.在本质上:

using System;
namespace Globals {
    public static class Commands {
        public static void WaitForReturn() {
            Console.WriteLine("Press Enter to continue.");
            Console.ReadLine();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

与上面类似,在任何其他类中,我可以通过将其包含为预处理指令来调用函数.

using System;
using Globals;

namespace RectangleDemo {
    <rectangle definition here>

    class Demo {
        static void Main(string[] args) {
            Rectangle r = new Rectangle();
            Rectangle s = new Rectangle(5, 6);
            r.Display();
            s.Display();
            WaitForReturn();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

实际上,我正在尝试简单地编译我的"实用程序"类(超过上面列出的内容)来检查错误,但它只是告诉我它无法编译它,因为没有主要方法.有什么建议?

(是的,我知道我有一个java编码风格,我没关系!)

Ayb*_*ybe 5

使用C#6,您可以导入静态方法,以便最终得到以下编码样式:

using System;
using static Globals.Commands;

namespace MyConsole
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            WaitForReturn();
        }
    }
}

namespace Globals
{
    public static class Commands
    {
        public static void WaitForReturn()
        {
            Console.WriteLine("Press Enter to continue.");
            Console.ReadLine();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

正如Tim所建议的那样,如果您不想要主入口点,请使用类库项目而不是控制台应用程序.