【问题标题】:Can't compare two string from extension methods in C# [duplicate]无法比较 C# 中扩展方法中的两个字符串 [重复]
【发布时间】:2021-04-07 22:43:48
【问题描述】:

我创建了一个字符串扩展方法

 public static string ShortID(this string id)
 {
     return id.Substring(5);
 }

当我想使用这样的扩展方法比较两个字符串时

if(Id.ShortID == IdDest.ShortID)

我有一个错误:

 CS0019: impossible to apply operator == to operands of type "group of methods" and "group of methods"

我不明白为什么,因为这个扩展方法返回一个字符串,逻辑上我可以将结果作为字符串比较 no ?

【问题讨论】:

  • 如果方法名后面不加括号,那只是方法组名,不是实际的方法调用。查看重复项。

标签: c# string compare extension-methods string-comparison


【解决方案1】:

您必须添加括号才能进行方法调用

if(Id.ShortID() == IdDest.ShortID())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 2021-01-08
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多