相关疑难解决方法(0)

c#中的引用类型和值类型有什么区别?

几个月前有人问我这个问题,我无法详细解释.C#中的引用类型和值类型有什么区别?

我知道,价值类型int,bool,float,等和引用类型delegate,interface等等.或者,这是不对的,太?

你能以专业的方式向我解释一下吗?

.net c# value-type reference-type

92
推荐指数
5
解决办法
8万
查看次数

C# - 装箱/拆箱/类型转换的问题.我不明白

我很难理解这一点.请考虑以下示例:

protected void Page_Load(object sender, EventArgs e)
{
    // No surprise that this works
    Int16 firstTest = Convert.ToInt16(0);
    int firstTest2 = (int)firstTest;

    // This also works
    object secondTest = 0;
    int secondTest2 = (int)secondTest;

    // But this fails!
    object thirdTest = Convert.ToInt16(0);
    int thirdtest2 = (int)thirdTest;  // It blows up on this line.
}
Run Code Online (Sandbox Code Playgroud)

我在运行时获得的具体错误是在Visual Studio中的Specified cast is not valid. If I QuickWatch (int)thirdTest,我得到的值为Cannot unbox 'thirdTest' as a 'int'.

到底发生了什么事?

c# boxing unboxing casting c#-4.0

8
推荐指数
2
解决办法
4944
查看次数

标签 统计

c# ×2

.net ×1

boxing ×1

c#-4.0 ×1

casting ×1

reference-type ×1

unboxing ×1

value-type ×1