相关疑难解决方法(0)

为什么在重写Equals方法时重写GetHashCode很重要?

鉴于以下课程

public class Foo
{
    public int FooId { get; set; }
    public string FooName { get; set; }

    public override bool Equals(object obj)
    {
        Foo fooItem = obj as Foo;

        if (fooItem == null) 
        {
           return false;
        }

        return fooItem.FooId == this.FooId;
    }

    public override int GetHashCode()
    {
        // Which is preferred?

        return base.GetHashCode();

        //return this.FooId.GetHashCode();
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经覆盖了该Equals方法,因为它Foo代表了Foos表的一行.哪个是覆盖的首选方法GetHashCode

覆盖为什么重要GetHashCode

c# overriding hashcode

1371
推荐指数
13
解决办法
35万
查看次数

.Net Framework中的字符串实习 - 有什么好处以及何时使用实习

我想知道特定于.Net框架的字符串实习的过程和内部.还想知道使用实习的好处以及我们应该使用字符串实习来提高性能的场景/情况.虽然我已经从Jeffery Richter的CLR书中学习实习,但我仍然感到困惑,并希望更详细地了解它.

[编辑]使用示例代码询问具体问题如下:

private void MethodA()
{
    string s = "String"; // line 1 - interned literal as explained in the answer        

    //s.intern(); // line 2 - what would happen in line 3 if we uncomment this line, will it make any difference?
}

private bool MethodB(string compareThis)
{
    if (compareThis == "String") // line 3 - will this line use interning (with and without uncommenting line 2 above)?
    {
        return true;
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

.net c# string performance string-interning

40
推荐指数
5
解决办法
1万
查看次数

.NET对象的内存开销是多少?

.NET中Object的内存开销是多少?我在谈论一个任意的简单对象......内部.NET工作或引用的开销:

var obj = new System.Object();
Run Code Online (Sandbox Code Playgroud)

obj在堆中占用了多少空间?

.net c#

35
推荐指数
1
解决办法
8873
查看次数

标签 统计

c# ×3

.net ×2

hashcode ×1

overriding ×1

performance ×1

string ×1

string-interning ×1