【问题标题】:how to call a file from Main method如何从 Main 方法调用文件
【发布时间】:2019-04-21 01:33:54
【问题描述】:

我刚刚开始学习 C# 并尝试做一些练习。

我想调用这个文件 就像我在 Main 方法中对其他文件所做的那样 但是因为它包含的方法很少

namespace ConsoleApplication1
{
    class StringCopies
    {
        static void Main(string[] args)
        {
            Console.WriteLine(test("C Sharp"));
            Console.WriteLine(test("JS"));
            Console.WriteLine(test("a"));
            Console.ReadLine();
        }
        public static string test(string str)
        {
            return str.Length < 2 ? str : str.Substring(0, 2) + str.Substring(0, 2) + str.Substring(0, 2) + str.Substring(0, 2);
        }
    }
}

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // new _strings_ZZaremaZ().Run();
            //new IfIntPositiveAndNegativeTrue().Run();
            //new computeIntIfEqualTriple().Run();
            //new ifParameterOrSumEquals20().Run();
            //new toLower().Run();
            //new longestWord().Run();
            //new SplitReverse().Run();
            //new FileSize().Run();
            //new MultiplyArrays().Run();
            //?????new StringLastItem().Run();
            //new StringContains().Run();
            //new MathMaxMinxyz().Run();
            //new toLower3firstToUpperifLess4().Run();
            //new ArraySum().Run();
            //new oneOfThemIs30orSum30().Run();
            //StringCopies myProgram = new StringCopies();

            Console.ReadKey();
        }
    }
}

【问题讨论】:

  • 请不要将您的代码包含为图像,而是包含文本(格式为代码示例)
  • ConsoleApplication1.StringCopies.Main(null) ?
  • 我想从“类程序”中调用“类 StringCopies”。如果可能的话
  • 我之前在其他课程上做过,但是它们是无效的,所以很容易
  • 请澄清您的具体问题或添加其他详细信息以准确突出您的需要。正如目前所写的那样,很难准确地说出你在问什么。请抽出时间拨打tour 并访问help center。您可能还想阅读How to Ask

标签: c# class main


【解决方案1】:

我想从“类程序”中调用“类 StringCopies”。如果它是 可能 – Ярослав Кудрявцев

是的,可能。将此代码放入程序的主要部分

StringCopies.Main(null);

喜欢:

class Program
{
    static void Main(string[] args)
    {
        // new _strings_ZZaremaZ().Run();
        //new IfIntPositiveAndNegativeTrue().Run();
        //new computeIntIfEqualTriple().Run();
        //new ifParameterOrSumEquals20().Run();
        //new toLower().Run();
        //new longestWord().Run();
        //new SplitReverse().Run();
        //new FileSize().Run();
        //new MultiplyArrays().Run();
        //?????new StringLastItem().Run();
        //new StringContains().Run();
        //new MathMaxMinxyz().Run();
        //new toLower3firstToUpperifLess4().Run();
        //new ArraySum().Run();
        //new oneOfThemIs30orSum30().Run();

        StringCopies.Main(null);

        Console.ReadKey();
    }
}

【讨论】:

  • 我的意思是将包含“static Void Main”和“public static test”函数的类放入Program.cs中
  • 它变得一团糟。我想为不同的课程提供不同的文件。例如,我使用的是“new MathMaxMinxyz().Run();”用一个 void 方法调用一个类。
  • 那么我的回答StringCopies.Main(null);有什么问题?
  • 当我将“StringsCopies.Main(null)”放入 Program.cs Main 时出现错误
  • 好的,什么错误?还是我必须猜测?如果您真的需要帮助,请查看how to ask a question.
猜你喜欢
  • 2021-12-21
  • 2017-02-13
  • 2014-06-09
  • 2013-11-28
  • 2013-05-16
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 2016-12-26
相关资源
最近更新 更多