c# - 为什么不能在另一个构造函数的主体中访问构造函数?

标签 c# constructor

我知道我可以像下面这样在另一个构造函数上访问构造函数,但是有没有办法在构造函数的主体中访问它?

public Rectangle(int size) : this(size, size)
{
    Console.WriteLine("Square Constructor Called");
    //this(size,size); i want to access like this 
}

最佳答案

你不能那样做。完整的理由是 here , 但总而言之:

In short, achieving the desired construction control flow is easy to do without adding the [the ability to call base constructors in arbitrary locations], and there's no compelling benefit to adding the feature. No new interesting representational power is added to the language.

关于c# - 为什么不能在另一个构造函数的主体中访问构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3423978/

相关文章:

c# - Log4Net 错误 : Failed to find configuration section 'log4net' in the application's . 配置文件

c# - C# 中的条件线程锁

java - 为什么不建议将硬编码的绝对路径名传递给 File 对象构造函数 File(String)

c++ - 错误 : no appropriate default constructor

c++ - 我定义了一个非拷贝构造函数;复制构造函数是否仍会被隐式定义?

java - 复制构造函数和多态性

java - super() 和 this() 可以出现在同一个构造函数中吗?

c# - 您的代码优先类是您的域类吗?

c# - .sln 文件丢失,那么如何运行一个有数据库连接的项目呢?

c# - WPF 应用程序中的 LINQ TO SQL 生成连接是关闭异常,有时