c# - 创建构造函数时报错 "member names cannot be the same as their enclosing type"是什么意思,如何解决?

标签 c# class oop constructor compiler-errors

当尝试从相同的类名为类创建构造函数时。 为什么会报错如下:

“错误:'StaveProcessor':成员名称不能与其封闭类型相同”

代码:

namespace ImageProcessing
{
    class StaveProcessor
    {
        public Bitmap stave;

        public StaveProcessor(Bitmap image) //constructor
        {
            stave = image;
        }
    }
}

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

ps:请自认为不是专家,请原谅我提出愚蠢的问题,帮助我学习和识别。谢谢

最佳答案

好吧,您提供的代码没有任何问题(它可以编译),如果您执行以下操作,将会显示您得到的异常:

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;
    }
}

关于c# - 创建构造函数时报错 "member names cannot be the same as their enclosing type"是什么意思,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17509603/

相关文章:

javascript - 如何正确使用socket.io和oop

c# - 如何写入现有的txt文件c#

php - 在里面插入sql数据库后表类不工作

javascript - 从 Django 模型创建 javascript 对象(类)

c# - 使用数组语法初始化我的类

PHP PDO fetchAll、PDO::FETCH_CLASS 和连接

C# 替代一堆基于层次结构的 else if 语句

c# - 为什么调试器内存窗口在 VS 2015 中不可用?

c# - WPF MultiBinding 到 Model.ChildProperty 不起作用?

c++ - 我必须创建一个对象才能在 C++ 中使用类方法吗?