Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
公共静态类中的c#循环_C#_Loops_Class_Static - Fatal编程技术网

公共静态类中的c#循环

公共静态类中的c#循环,c#,loops,class,static,C#,Loops,Class,Static,我是c#新手,试图在课堂上做循环,但不知道怎么做;五 需要这样做: namespace MyApp { using [...] public static class MyClass { private const string Options = [...]; [Option("option#")] public static void Option#([...]) { [.

我是c#新手,试图在课堂上做循环,但不知道怎么做;五

需要这样做:

namespace MyApp
{
    using [...]


    public static class MyClass
    {
        private const string Options = [...];


        [Option("option#")]
        public static void Option#([...])
        {
            [...]
        }



        # is value from for loop {i} and below is inside loop 
        e.g.

        for (int i = 0; i < 5; i++)
        {

            [Option("option{i from loop}")]
            public static void Option{i from loop}([...])
            {
                My code
            }

        }
名称空间MyApp
{
使用[…]
公共静态类MyClass
{
私有常量字符串选项=[…];
[选项(“选项”)
公共静态无效选项#([…])
{
[...]
}
#是来自for循环{i}的值,下面是循环内的值
例如
对于(int i=0;i<5;i++)
{
[选项(“选项{i from loop}”)]
公共静态无效选项{i from loop}([…])
{
我的代码
}
}
如何实现这一点?我需要在循环中将command/public添加到类MyClass.loop generate command和public for command中,并在运行编译的.exe文件时将其添加到类中,而不是在生成.exe文件时 欢迎大家的帮助,一定要学习;)

公共静态void MyAwesomeChangingMethod(inti)
{
控制台写入线(i);
}
...
//现在你可以用不同的号码多次呼叫它
MyAwesomeChangingMethod(1);
MyAwesomeChangingMethod(3);
MyAwesomeChangingMethod(675675);
//或
对于(int i=0;i<5;i++)
MyAwesomeChangingMethod(i);

注意:如果这不是您想要的,您确实需要更好地解释您的问题(而不是在注释中)

下面是一个添加字符串(以及数字)的
静态类的示例通过名为
AddCommands
的方法创建私有列表,该方法使用
for
循环。它使用
foreach
循环显示
showcomands
方法中的命令:

static class Commander
{
    private static List<string> Commands;

    public static void AddCommands(string command, int count)
    {
        if (Commands == null) Commands = new List<string>();

        int startValue = Commands.Count + 1;
        int endValue = startValue + count;

        for (int i = startValue; i < endValue; i++)
        {
            Commands.Add(command + i);
        }
    }

    public static void ShowCommands()
    {
        if ((Commands?.Any()).GetValueOrDefault())
        {
            foreach (var command in Commands)
            {
                Console.WriteLine(command);
            }
        }
        else
        {
            Console.WriteLine("There are no commands available.");
        }

        Console.WriteLine("-------------------\n");
    }
}
输出


循环中包含了
公共静态
。您需要它。
公共静态
将包含循环。停止!离开键盘,请开始阅读基础知识,也可以,谢谢:)阅读时间开始上课到底应该做什么?可以吗你提供了一个更具体的例子来说明你想要什么?通常循环和其他代码执行都发生在类中包含的方法内部。哇,对于这样一个低质量和模糊的问题,我需要一些类似t4模板的东西,但是当我启动内置应用程序时。自动生成命令,例如(inti=0;i<5;i++){[Option(“Option{i from loop}”)]public static void Option{i from loop}([…]){My code}
当我启动.exe时,其中包含的loop add命令/public将被添加到类中x次。这不是关于打印文本,只是在类中键入文本以使程序将其作为command@Nitr0o你意识到这些信息都不在你的问题中吗?这是相当重要的东西,你真的需要大量更新你的问题更多信息你,我必须准确地说明这个问题,因为只有我知道我想要什么;v对不起
static class Commander
{
    private static List<string> Commands;

    public static void AddCommands(string command, int count)
    {
        if (Commands == null) Commands = new List<string>();

        int startValue = Commands.Count + 1;
        int endValue = startValue + count;

        for (int i = startValue; i < endValue; i++)
        {
            Commands.Add(command + i);
        }
    }

    public static void ShowCommands()
    {
        if ((Commands?.Any()).GetValueOrDefault())
        {
            foreach (var command in Commands)
            {
                Console.WriteLine(command);
            }
        }
        else
        {
            Console.WriteLine("There are no commands available.");
        }

        Console.WriteLine("-------------------\n");
    }
}
class Program
{
    private static void Main()
    {
        Console.WriteLine("Before adding any commands the list looks like:");
        Commander.ShowCommands();

        Commander.AddCommands("SomeCommand", 5);
        Console.WriteLine("After adding 5 commands the list looks like:");
        Commander.ShowCommands();

        Commander.AddCommands("AnotherCommand", 5);
        Console.WriteLine("After adding 5 more commands the list looks like:");            
        Commander.ShowCommands();

        Console.WriteLine("Done! Press any key to exit...");
        Console.ReadKey();
    }
}