Visual Studio的C#intellisense可以给出一个提示,首先显示某个方法过载吗?

我有两种相互重载的方法

public class Car { public int GetPrice(string vinNumber) { string make = Database.GetMake(vinNumber); // expensive operation string model = Database.GetModel(vinNumber); // expensive operation int year = Database.GetYear(vinNumber); // expensive operation return this.GetPrice(make, model, year); } public int GetPrice(string make, string model, int year) { // Calculate value and return } } 

在我的示例中,GetPrice(make,model,year)重载执行起来很便宜,但GetPrice(vinNumber)方法很昂贵。 问题是昂贵的方法具有最少的参数并且它首先出现在C#intellisense中。

这两种方法都是有效的,但我想鼓励人们称之为便宜的方法。 但是在选择要调用的方法之前,人们往往不会查看Intellisense中的所有重载,并且在我公司的代码库中经常调用昂贵的重载。

有没有办法告诉Visual Studio为特定方法提供“intellisense优先级”,以便它首先显示?

不要这么认为。

除非您编写智能插件(如Resharper)并劫持默认智能感知并为用户创建程序以分配优先级。

  1. XML注释中的摘要标记显示在Intellisense中。
  2. 您可以使用Obsolete标记装饰该方法,该标记也会根据设置生成警告或错误。

     [System.Obsolete("use GetPrice(make, model, year)")] 

这是怎么回事:

  • 当您键入成员或在列表中突出显示它时,您看到的单个重载是代码中首先列出的重载。
  • 在您接受该成员并且在括号内之后,该顺序似乎基于参数的数量,从最少到最多。

您可能会考虑做的是,而不是重载,在开始时将成员命名为相同,在结尾处命名不同( GetMake vs GetMakeSlow ,但显然比这更好),因此它们在Intellisense中一起显示但是它已经传达了您应该使用的。

否则,使它们成为真正的重载,但使用XML文档在慢速上发出明确的警告。

我能提供的唯一解决方案是评论,但这并不意味着用户会关注它们:

  ///  /// This method should be used as a last resort... ///  ///  ///  public int GetPrice(string vinNumber) { ... } ///  /// This is the preferred method... ///  ///  ///  ///  ///  public int GetPrice(string make, string model, int year) { ... } 

编辑:我试过这个没有任何区别:

 class Class1 { public static void Method(int value1) { } public static void Method(int value1, int value2) { } public static void Method(int value1, int value2, int value3) { } } class Class2 { public static void Method(int value1, int value2, int value3) { } public static void Method(int value1, int value2) { } public static void Method(int value1) { } }