枚举作为C#函数的参数

时间:2016-03-12 12:06:57

标签: c# enums void

我正在尝试编写一个用char写入文本char的程序。我想用enum选择写入速度。但它会引发错误。所以,这是我的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace WritingApp
{
    class Program
    {
        public enum WriteMode { Slow, SlowRandom, Normal, NormalRandom, Fast, FastRandom };

        static void Main(string[] args)
        {
            MessageWrite("smth", WriteMode.Fast); //Error here
//An object reference is required for the non-static field, method, or property   
//'Program.MessageWrite(string, Program.WriteMode)'
        }

        void MessageWrite(string message, WriteMode mode)
        {
            for(int i = 0; i < message.Length; i++)
            {
                Console.Write(message[i]);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

你应该MessageWrite静态。

static void MessageWrite(string message, WriteMode mode)
{
    for (int i = 0; i < message.Length; i++)
    {
        Console.Write(message[i]);
    }
}

查看Why static methods can't call non-static methods directly?