English 中文(简体)
C# 超载操作员=和!
原标题:C# Overloading Operator == and !=

我遇到的问题是,在少数几类课堂和交接点中,找不到理想的行为。

我的问题是:

//Inside a Unit Test that has access to internal methods and properties

INode  firstNode, secondNode;

INodeId  id = new NodeId (4);

first = new Node (id, "node");
second = new Node (id, "node");

Assert.IsTrue (first == second);

上文的说法之所以失败,是因为似乎将目标类别同起来,而不是在Node和NodeId班上超负荷运行。

如果你对我如何获得理想行为提出任何建议,那将是很麻烦的。

这里是我所致力于的框架的一部分:

public interface IIdentifier<T> where T : class
{
    TKeyDataType GetKey<TKeyDataType> ();

    bool Equals (IIdentifier<T> obj;
}

public interface INode
{
    string name
    {
        get;
    }

    INodeId id
    {
        get;
    }
}

public interface INodeId : IIdentifier<INode>
{
}

public class Node : INode
{
    internal Node(INodeId  id, string name)
    { 
       //Work
    }

    public static bool operator == (Node n1, Node n2)
    {
        return n1.equals(n2);
    }

    public static bool operator != (Node n1, Node n2)
    {
        return !n1.equals(n2);
    }

    public bool Equals (INode  node)
    {
        return this.name == node.name &&
               this.id = node.id;
    }

    #region INode Properties

    }

public class NodeId : INodeId
{

    internal NodeId(int id)
    { 
       //Work
    }

    public static bool operator == (NodeId  n1, NodeId  n2)
    {
        return n1.equals(n2);
    }

    public static bool operator != (NodeId  n1, NodeId  n2)
    {
        return !n1.equals(n2);
    }

    public override bool Equals (object obj)
    {
        return this.Equals ((IIdentifier<INode>) obj);
    }

    public bool Equals (IIdentifier<INode> obj)
    {
        return obj.GetKey<int>() ==  this.GetKey<int>();
    }

    public TKeyDataType GetKey<TKeyDataType> ()
    {
        return (TKeyDataType) Convert.ChangeType (
            m_id,
            typeof (TKeyDataType),
            CultureInfo.InvariantCulture);
    }


    private int m_id;

}
最佳回答

操作员超负荷在根据所申报的操作类型而不是在操作时间的实际类型进行汇编时得到解决。 另一种说法是,经营者超负荷是虚拟的。 因此,您在上文所作的比较是INode.operator=,而不是Node.operator=。 自INode.operator= 页: 1 反对:operator=,仅作参考比较。

在这个问题上没有真正的好办法。 最为正确的做法是使用<代码>Equals(>,而不是=,而不论在哪里,歌剧可能都是物体。 如果你真的需要一台振动的虚拟操作员超负荷工作,你应界定<条码>operator ==。 Equals。 然而,我们注意到,这为接口赢得了一定的工作,这是你的工作。

问题回答

我认为,你可能需要在诺德像你一样,在诺德推翻平等(目标)。 因此:

public override bool Equals (object obj)
{
    if (obj is Node)
    {
        return this.Equals(obj as Node);
    }
    return false;
}

// your code (modified to take a Node instead of an INode)
public bool Equals (Node  node)
{
    return this.name == node.name &&
           this.id = node.id;
}

它使用 = 物体,因为首先 Node and secondNode aren t of category Node, they re-pic INode. 汇编者不承认基本类型。

由于你能够在一个接口中超载一个运营商,你的最佳信条或许是重新制定该守则,以便它不使用INode接口。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签