111,074
社区成员




using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication9
{
public class B//class版本
{
public int i;
}
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();
List<B> listClass = new List<B>();
for (int i = 0; i < 10000; i++)
{
B b = new B();
b.i = i;
list.Add(b.GetHashCode());
listClass.Add(b);
}
Console.WriteLine(list.Distinct().Count());//9999
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication9
{
public struct B//struct版本
{
public int i;
}
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();
List<B> listClass = new List<B>();
for (int i = 0; i < 10000; i++)
{
B b = new B();
b.i = i;
list.Add(b.GetHashCode());
listClass.Add(b);
}
Console.WriteLine(list.Distinct().Count());//10000
Console.ReadKey();
}
}
}
using System;
using System.Linq;
using System.Collections.Generic;
class B
{
//public int i;
}
class MyClassA
{
static bool Compare<T>(T obj1, T obj2)
where T:class
{
return obj1 == obj2;
}
static void Main1(string[] args)
{
int count = 1*10000;
List<int> listHashcode = new List<int>();
List<B> listObject = new List<B>();
for (int i = 0; i < count; i++)
{
B b = new B();
listHashcode.Add(b.GetHashCode());
listObject.Add(b);
}
Console.WriteLine("listHashcode:{0}", listHashcode.Distinct().Count());
Console.WriteLine("listObject:{0}", listObject.Distinct().Count());
Console.ReadKey();
}
}
using System;
class MultiUseWord
{
internal UIntPtr value;
internal const uint INFLATED_TAG = 3;
internal static int GetHashCode(Object obj)
{
int result;
//!!!-1
MultiUseWord muw = GetForObject(obj);
uint tag = muw.GetTag();
result = (int)(muw.GetPayload());
return result;
}
[ManualRefCounts]
internal static MultiUseWord GetForObject(Object obj)
{
//!!!-2
MultiUseWord result = ((ObjectPreMUW)obj).muw;
return result;
}
internal uint GetTag()
{
uint tag = (uint)(this.value & INFLATED_TAG);
return tag;
}
internal UIntPtr GetPayload()
{
UIntPtr result = (this.value & PAYLOAD_MASK);
return result;
}
internal static UIntPtr PAYLOAD_MASK
{
get
{
return (UIntPtr.Size == 8) ?
(UIntPtr)0xfffffffffffffff8 :
(UIntPtr)0xfffffff8;
}
}
}
// This class does not add any fields, nor should it ever be
// referenced by any other code. Its sole purpose is to add
// implementations of the get_muw, set_muw, and compareExchangeMUW
// methods to all objects when they have a MultiUseWord field.
[MixinConditional("ObjectHeaderDefault")]
[MixinConditional("ObjectHeaderPostRC")]
[Mixin(typeof(ObjectMUW))]
internal class ObjectPreMUW : ObjectMUW
{
internal new PreHeaderDefault preHeader;
internal new MultiUseWord muw
{
//!!!-3
[Inline]
get { return this.preHeader.muw; }
[Inline]
set { this.preHeader.muw = value; }
}
[Inline]
internal new UIntPtr CompareExchangeMUW(UIntPtr newValue,
UIntPtr oldValue)
{
return Interlocked.CompareExchange(ref this.preHeader.muw.value,
newValue, oldValue);
}
}
[MixinConditional("ObjectHeaderDefault")]
[MixinConditional("ObjectHeaderPostRC")]
[Mixin(typeof(PreHeader))]
[RequiredByBartok]
internal struct PreHeaderDefault /* : PreHeader */
{
//!!!-4
internal MultiUseWord muw;
}
// This class does not add any fields, nor should it ever be
// referenced by any other code. Its sole purpose is to declare
// that all objects have get_muw, set_muw, and compareExchangeMUW
// methods when the object header includes a MultiUseWord field.
[MixinConditional("ObjectHeaderDefault")]
[MixinConditional("ObjectHeaderPostRC")]
[Mixin(typeof(System.Object))]
internal class ObjectMUW
{
internal extern MultiUseWord muw
{
[Inline]
get;
[Inline]
set;
}
[Inline]
internal extern UIntPtr CompareExchangeMUW(UIntPtr newValue,
UIntPtr oldValue);
}
class Test
{
static void Main(string[] args)
{
object obj = new object();
MultiUseWord.GetHashCode(obj);
}
}