无法访问方法

时间:2015-02-22 21:03:28

标签: c# methods

我遇到以下代码错误。我无法调用ShowUp()函数。类Dx是在错误的位置还是声明了错误的方法?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Program1
{
    class Program    
    {
        public class Dx
        {
             void ShowUp()
            {
                Console.Write("Show");
                Console.Read();
                return;
            }
        }

        static void Main(string[] args)
        {
            Dx.ShowUp();
        }               
    }
}

1 个答案:

答案 0 :(得分:4)

该功能不是静态的。

因此,要么更改函数定义,要么创建实例。而且你的职能也需要内部或公开,正如@uguraldanmaz所说:

  1. 将您的功能更改为静态功能

    public static void ShowUp()

  2. 创建类的实例

    var dx = new Dx(); dx.ShowUp();