是否有一种紧凑的方式告诉 C# 编译器使用基本 Equals 和 == 运算符?

发布于 2024-12-06 12:46:22 字数 1317 浏览 1 评论 0原文

我对 C# 相当陌生,并且有 C++ 背景。

我已经定义了一个结构体,并且(Microsoft)编译器不断弹出错误 CA1815“'GenericSendRequest' 应该覆盖 Equals”

我读了一下,发现 C# 结构体派生自 ValueType,它使用反射实现了通用 Equals。这让我更加困惑:

  1. 如果这只是一个性能问题,为什么编译器会创建错误而不是警告?
  2. 如果它不允许您使用它,为什么它首先要定义通用 Equals 呢?

那么我怎样才能告诉编译器“我不在乎”呢?类似于仅在 C++ 类中声明赋值运算符而不提供定义来确认我知道我在做什么。

到目前为止,我的解决方案是在我的结构中包含:

    public static bool operator ==(GenericSendRequest lhs, GenericSendRequest rhs)
    {
        return lhs.Equals(rhs);
    }

    public static bool operator !=(GenericSendRequest lhs, GenericSendRequest rhs)
    {
        return !lhs.Equals(rhs);
    }

    public override bool Equals(object obj)
    {
        return base.Equals(obj);
    }

    //Yes, it also makes me override GetHashCode since I'm overriding Equals.
    public override int GetHashCode()
    {
        return base.GetHashCode();
    }

,这太糟糕了。

编辑: 这是结构体定义:

public struct GenericSendRequest
{
        public LiveUser             Sender;
        public LiveUser[]           Receivers;
        public Message              Msg;
        public ServiceHttpRequest   HttpRequest;
}

它的用法只是函数的多个返回值:

public static GenericSendRequest CreateGenericSendRequest(...);

I am fairly new to C#, and I come from a C++ background.

I have defined a struct, and the (Microsoft) compiler keeps popping up the error CA1815 "'GenericSendRequest' should override Equals"

I read a bit around and saw that C# structs derive from ValueType which impleents a generic Equals using reflection. This confused me more:

  1. Why does the compiler create an error instead of a warning if its just a performance issue?
  2. Why does it define that generic Equals in the first place if it's not going to let you use it?

So how can I tell the compiler that "I don't care"? Something similar with just declaring assignment operator in a C++ class without providing definition to acknowledge that I know what I am doing.

So far my solution has been to include:

    public static bool operator ==(GenericSendRequest lhs, GenericSendRequest rhs)
    {
        return lhs.Equals(rhs);
    }

    public static bool operator !=(GenericSendRequest lhs, GenericSendRequest rhs)
    {
        return !lhs.Equals(rhs);
    }

    public override bool Equals(object obj)
    {
        return base.Equals(obj);
    }

    //Yes, it also makes me override GetHashCode since I'm overriding Equals.
    public override int GetHashCode()
    {
        return base.GetHashCode();
    }

in my struct, which is just awful.

Edit:
This is the struct definition:

public struct GenericSendRequest
{
        public LiveUser             Sender;
        public LiveUser[]           Receivers;
        public Message              Msg;
        public ServiceHttpRequest   HttpRequest;
}

Its usage is just multiple return values from a function:

public static GenericSendRequest CreateGenericSendRequest(...);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苦行僧 2024-12-13 12:46:22

这绝对不是一个错误,它只是一个警告 - 而且只有当您在构建过程中启用了代码分析时,该警告才会显示。这是性能优化的建议 - 就这样吧。

编辑:

事实证明这是可配置的:

转到项目属性|代码分析 | 运行此规则集.. 打开

展开性能部分 - 对于 CA 1815,您可以选择是否需要此功能是警告、错误或无。

在此处输入图像描述

This is definitely not an error, its only a warning - and that warning even only will show up if you have enabled code analysis as part of your build. It's a suggestion for performance optimization - take it that way.

Edit:

Turns out this is configurable:

Go to Project Properties | Code Analysis | Run this rule set.. Open

Expand the Performance section - for CA 1815 you can select whether you want this to be a warning, an error or none.

enter image description here

去了角落 2024-12-13 12:46:22

不知怎的,你在 IDE 中有点迷失了,这不是编译器错误。这是一个代码分析错误,由名为 FxCop 的工具执行。您可以使用分析+配置来禁用它,取消选中“在构建时启用代码分析”选项。

这个工具有点麻烦,它的使用更多的是提醒您可能做了一些不明智的事情。在这种情况下这是不太可能的,这不是一种无需做大量工作就可以进行有意义比较的结构。这是一个性能警告,结构的默认相等比较器使用反射,这不是很有效。但是通过实现正确版本的 Equals() 会大大降低效率。

还有一些问题,C# 中的结构体的行为根本不像 C++ 中的结构体。它应该只用于可以轻松复制的简单值,因为它是一种值类型。你应该把它变成一个类。也解决了分析错误。

You got a little lost in the IDE somehow, this is not a compiler error. It is a code analysis error, performed by a tool known as FxCop. You can disable it with Analyze + Configure, untick the "Enable Code Analysis on Build" option.

The tool is a little naggy, its use is more as a reminder that you might have done something unwise. Which in this case is pretty unlikely, this is not the kind of struct you can meaningfully compare without doing a lot of work. It is a performance warning, the default equality comparer for a struct uses reflection and that's not very efficient. But you'll make it a lot less efficient by implementing a correct version of Equals().

There is something else wrong, a struct in C# does not at all behave like a struct in C++. It should only be used for simple values that can be easily copied, given that it is a value type. You should make this a class instead. Solves the analysis error too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文