Can't operator == be applied to generic types in C#?

前端 未结 12 671
囚心锁ツ
囚心锁ツ 2020-11-22 02:21

According to the documentation of the == operator in MSDN,

For predefined value types, the equality operator (==) returns true if th

12条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 02:49

    So many answers, and not a single one explains the WHY? (which Giovanni explicitly asked)...

    .NET generics do not act like C++ templates. In C++ templates, overload resolution occurs after the actual template parameters are known.

    In .NET generics (including C#), overload resolution occurs without knowing the actual generic parameters. The only information the compiler can use to choose the function to call comes from type constraints on the generic parameters.

提交回复
热议问题