在主program.cs文件中运行另一个.cs文件

时间:2016-09-10 15:38:00

标签: c# console

我对编程很新,所以我确定这个问题很天真。

所以,我写了一个简单的魔术八球计划,我想进入一个冒险游戏,但我认为在游戏中加入八球计划是很好的,就像制作一个控制台菜单,你可以选择游戏。

所以,我的问题是,我不知道如何将我的八球程序放在一个文件中,然后从主程序文件中运行它,如

eightball();

如果这可以简单地完成,我是否也可以在if语句中使用它,比如

if (response == 1)
{
eightball();
}

谢谢大家的帮助!

此外,我确信我在研究中遇到了答案,但我可能不理解。

2 个答案:

答案 0 :(得分:0)

查看classes

您可能想要创建一个新的类文件,然后添加如下代码:

namespace MyApplication
{
    class EightBall
    {
        public static void eightball ()
        {
            // Add your code here
        }
    }
}

从您的Main调用它 - 这样的功能:

if (response == 1)
{
    EightBall.eightball();
}

答案 1 :(得分:-1)

namespace MyApplication
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new /*the name of your program*/());
        }
    }
}