私有构造函数的奇怪的intellisense行为

时间:2022-01-21 17:26:02

Example:

public class Name {

    public string FirstName { get; private set; }
    public string LastName { get; private set; }

    private Name() { }

    public Name(string firstName, string lastName) {

        FirstName = firstName;
        LastName = lastName;

    }  
}

When trying to instantiate this c# class, intellisense shows both the private and the public constructor for new keyword even though one of the constructor is private!

当试图实例化这个c#类时,intellisense会显示new关键字的私有和公共构造函数,即使其中一个构造函数是私有的!

What is even more weird is that when I remove the second argument from the public constructor ( remove lastName as argument to public constructor), intellisense now shows just the public constructor with new keyword, correctly.

更奇怪的是,当我从公共构造函数中删除第二个参数时(删除lastName作为公共构造函数的参数),intellisense现在只显示带有new关键字的公共构造函数。

Is this a bug or am I missing something here? I am using VS2008 SP1.

这是一个错误还是我错过了什么?我正在使用VS2008 SP1。

edit: code clarity

编辑:代码清晰度

4 个解决方案

#1


1  

Wow, that's strange. I just tried it myself on my copy of VS2008 (I'm also running SP1) and had the exact same results. When there was more than one parameter, the private constructor showed up in Intellisense, but not when there was only one. My guess is, it's a bug.

哇,这很奇怪。我只是在我的VS2008副本上试了一下(我也在运行SP1)并且得到了完全相同的结果。当有多个参数时,私有构造函数出现在Intellisense中,但不会出现在只有一个参数时。我的猜测是,这是一个错误。

#2


0  

Don't know why intellisense is showing you weird things. But you should have an abstract base class for you domain objects with a public constructor, so you don't have to pepper your objects with the private ones. You should also put there things like your properties for your primary keys and such.

不知道为什么intellisense会向你展示奇怪的东西。但是,对于具有公共构造函数的域对象,您应该有一个抽象基类,因此您不必使用私有对象来对象。你还应该把你的主要属性等东西放在那里。

public abstract class BaseDomainObject{
  public BaseDomainObject() { }

  private int _id;

  public virtual int Id { get { return _id; } set { _id = value; } }

}

public SomeDomainObject : BaseDomainObject{
  ...
}

#3


0  

It's likely a bug but not worth the effort to fix. Namely because there are numerous scenarios where accessing a private constructor is legal. Take the following snippet of code. All private constructor access is legal

这可能是一个错误但不值得修复的努力。也就是因为有很多场景访问私有构造函数是合法的。请使用以下代码段。所有私有构造函数访问都是合法的

class Outer {
  private Outer() {
  }
  public Outer Create() { return new Outer(); }
  class Inner() { 
    void Function1() { new Outer(); }
    class DoubleInner() {
       void Function2() { new Outer(); }
    }
  }
}

#4


0  

Even though the private constructor shows up in Intellisense the Compiler will still through an "inaccessible due to protection level" error if you try to compile code that uses it where it's not permitted

即使私有构造函数出现在Intellisense中,如果您尝试编译在不允许的情况下使用它的代码,编译器仍将通过“由于保护级别而无法访问”错误

#1


1  

Wow, that's strange. I just tried it myself on my copy of VS2008 (I'm also running SP1) and had the exact same results. When there was more than one parameter, the private constructor showed up in Intellisense, but not when there was only one. My guess is, it's a bug.

哇,这很奇怪。我只是在我的VS2008副本上试了一下(我也在运行SP1)并且得到了完全相同的结果。当有多个参数时,私有构造函数出现在Intellisense中,但不会出现在只有一个参数时。我的猜测是,这是一个错误。

#2


0  

Don't know why intellisense is showing you weird things. But you should have an abstract base class for you domain objects with a public constructor, so you don't have to pepper your objects with the private ones. You should also put there things like your properties for your primary keys and such.

不知道为什么intellisense会向你展示奇怪的东西。但是,对于具有公共构造函数的域对象,您应该有一个抽象基类,因此您不必使用私有对象来对象。你还应该把你的主要属性等东西放在那里。

public abstract class BaseDomainObject{
  public BaseDomainObject() { }

  private int _id;

  public virtual int Id { get { return _id; } set { _id = value; } }

}

public SomeDomainObject : BaseDomainObject{
  ...
}

#3


0  

It's likely a bug but not worth the effort to fix. Namely because there are numerous scenarios where accessing a private constructor is legal. Take the following snippet of code. All private constructor access is legal

这可能是一个错误但不值得修复的努力。也就是因为有很多场景访问私有构造函数是合法的。请使用以下代码段。所有私有构造函数访问都是合法的

class Outer {
  private Outer() {
  }
  public Outer Create() { return new Outer(); }
  class Inner() { 
    void Function1() { new Outer(); }
    class DoubleInner() {
       void Function2() { new Outer(); }
    }
  }
}

#4


0  

Even though the private constructor shows up in Intellisense the Compiler will still through an "inaccessible due to protection level" error if you try to compile code that uses it where it's not permitted

即使私有构造函数出现在Intellisense中,如果您尝试编译在不允许的情况下使用它的代码,编译器仍将通过“由于保护级别而无法访问”错误