什么是"成员名称不能与其封闭类型相同"的错误意味着在创建构造函数时,以及如何解决它?

hir*_*sht 1 c# oop constructor compiler-errors class

在为同一个类名创建类的构造函数时.为什么会抛出错误,如下所示:

" Error: 'StaveProcessor': member names cannot be the same as their enclosing type"

代码:

namespace ImageProcessing
{
    class StaveProcessor
    {
        public Bitmap stave;

        public StaveProcessor(Bitmap image) //constructor
        {
            stave = image;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题并创建构造函数?

ps:请考虑我自己不是专家,请原谅我提出愚蠢的问题,并帮助我学习和识别.谢谢

Dim*_*rov 5

那么你提供的代码没有任何问题(编译),如果你做了类似下面的事情,你会得到的例外:

class StaveProcessor
{
    // can't have this method with this name (note the void, it's not a constructor)
    public void StaveProcessor(Bitmap image)
    {
        stave = image;
    }
}
Run Code Online (Sandbox Code Playgroud)