结构,接口和拳击 [英] Structs, Interfaces and Boxing

查看:147
本文介绍了结构,接口和拳击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  它是安全的结构实现接口?

把这个code:

interface ISomeInterface
{
    public int SomeProperty { get; }
}

struct SomeStruct : ISomeInterface
{
    int someValue;

    public int SomeProperty { get { return someValue; } }

    public SomeStruct(int value)
    {
        someValue = value;
    }
}

然后我这样做的话:

and then I do this somewhere:

ISomeInterface someVariable = new SomeStruct(2);

SomeStruct 盒装在这种情况下?

推荐答案

是的,是的。基本上,只要你需要的参考的和你只得到一个值类型值,该值是盒装。

Yes, it is. Basically whenever you need a reference and you've only got a value type value, the value is boxed.

在这里, ISomeInterface 是一个接口,这是一个引用类型。因此价值 someVariable 始终是一个参考,因此新创建的结构值已被装箱。

Here, ISomeInterface is an interface, which is a reference type. Therefore the value of someVariable is always a reference, so the newly created struct value has to be boxed.

这篇关于结构,接口和拳击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆