类型 '...' 没有定义构造函数 [英] The type '...' has no constructors defined

查看:27
本文介绍了类型 '...' 没有定义构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到当我错误地尝试实例化特定类时生成的编译器错误类型..."没有定义的构造函数.

I'm noticing the compiler error The type '...' has no constructors defined generated when I erroneously attempt to instantiate a particlar class.

这让我想知道我将如何编写我自己的类,当有人试图实例化它时会沉淀这个消息.

It lead me to wonder how I would go about writing my own class that would precipitate this message when someone attempted to instantiate it.

那么下面的代码,我需要对MyClass做什么?

So the code below, what do I need to do to MyClass?

namespace MyNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();
        }
    }

    class MyClass
    {
        MyClass()
        {
        }
    }
}

推荐答案

此错误 (CS0143) 如果该类只定义了一个内部构造函数,然后您尝试从另一个程序集中实例化它.

This error (CS0143) occurs if the class only defines an internal constructor and you try to instantiate it from another assembly.

public class MyClass
{
    internal MyClass()
    {
    }
}

这篇关于类型 '...' 没有定义构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆