最近我在徘徊,是否有可能通过引用比较结构。读取结构变量会创建该变量的副本,因此按引用比较标准结构似乎是不可能的。但是,使用C#7 ref
会更有意义。
我定义了4个变量
MyStruct foo = new MyStruct(){SomeInt = 1};
ref MyStruct refFoo = ref foo;
MyStruct bar = new MyStruct() { SomeInt = 2 };
ref MyStruct refBar = ref foo;
struct MyStruct
{
public int SomeInt { get; set; }
}
当我尝试这样做时:var comparison1 = ReferenceEquals(refFoo, refBar);
,我得到警告,说该值始终为false,因为我正在传递值类型。
ref struct
ref struct MyStruct
{
public int SomeInt { get; set; }
}
尝试以下操作时:var comparison1 = ReferenceEquals(refFoo, refBar);
,我收到一个编译错误,说MyStruct
无法分配给参数类型object
。如果我尝试也做同样的事情:var comparison1 = ReferenceEquals(foo, bar);
MyStruct
是ref struct
时,但是声明了refFoo
和refBar
变量而没有ref
。 (我遇到的错误与第二种情况相同)static void Main(string[] args)
{
MyStruct foo = new MyStruct(){SomeInt = 1};
MyStruct refFoo = foo;
MyStruct bar = new MyStruct() { SomeInt = 2 };
MyStruct refBar = foo;
var comparison1 = ReferenceEquals(refFoo, refBar);
}
ref struct MyStruct
{
public int SomeInt { get; set; }
}
答案 0 :(得分:1)
var comparison1 = ReferenceEquals(refFoo, refBar);
,我收到警告说该值始终为false,因为我正在传递值类型
每个值都将用方框框起来,因此结果始终为假。
var comparison1 = ReferenceEquals(refFoo, refBar);
,我收到一个编译错误,指出MyStruct无法分配给参数类型对象。如果我尝试也做同样的事情:var comparison1 = ReferenceEquals(foo, bar);
ref
不能装箱或拆箱(就像@PetSerAl写道)。
最后一种情况是MyStruct是ref struct,但refFoo和refBar变量声明为不带ref。 (我遇到的错误与第二种情况相同)
与上述相同。
奖金问题:为什么只有第二和第三示例给我一个编译错误?
我确定你现在明白了。
那么,有一些偷偷摸摸的方法可以通过引用比较结构吗?
如果您要比较地址,请参考以下示例:
class Program
{
struct Struct { public int Value { get; set; } }
ref struct RefStruct { public int Value { get; set; } }
static unsafe void Main(string[] args)
{
var @struct = new Struct { Value = 5 };
var struct2 = @struct;
ref Struct struct3 = ref @struct;
Struct* p = &@struct;
Struct* p2 = &struct2;
Console.WriteLine($"struct address is: {(int)p} struct value is {p->Value}");
Console.WriteLine($"struct2 address is: {(int)p2} struct2 value is {p2->Value}");
fixed (Struct* p3 = &struct3)
{
Console.WriteLine($"struct3 address is: {(int)p3} struct3 value is {p3->Value}");
}
Console.WriteLine();
Console.WriteLine($"struct and struct2 Unsafe.AreSame? {Unsafe.AreSame(ref @struct, ref struct2)}");
Console.WriteLine($"struct and struct3 Unsafe.AreSame? {Unsafe.AreSame(ref @struct, ref struct3)}");
Console.WriteLine();
var structAsPointer = Unsafe.AsPointer(ref @struct);
var struct2AsPointer = Unsafe.AsPointer(ref struct2);
var struct3AsPointer = Unsafe.AsPointer(ref struct3);
Console.WriteLine($"struct AsPointer and struct2 AsPointer are same? {structAsPointer == struct2AsPointer}");
Console.WriteLine($"struct AsPointer and struct3 AsPointer are same? {structAsPointer == struct3AsPointer}");
Console.WriteLine();
var refStruct = new RefStruct { Value = 7 };
var refStruct2 = refStruct;
RefStruct* p4 = &refStruct;
RefStruct* p5 = &refStruct2;
Console.WriteLine($"refStruct address is: {(int)p4}, refStruct value is: {p4->Value}");
Console.WriteLine($"refStruct2 address is: {(int)p5}, refStruct value is: {p5->Value}");
ref RefStruct refStruct3 = ref refStruct;
fixed (RefStruct* p6 = &refStruct3)
{
Console.WriteLine($"refStruct3 address is: {(int)p6}, refStruct3 value is: {p6->Value}");
Console.WriteLine();
Console.WriteLine($"refStruct and refStruct2 are same? {&refStruct == &refStruct2}");
Console.WriteLine($"refStruct and refStruct3 are same? {&refStruct == p6}");
}
}
}