当@No One 回答我的问题时here他在下面提到了类似的东西,
using(SomeClass c = new SomeClass())
{ }
**
will translate to
**
try
{
SomeClass c = new SomeClass();
}
finally
{
c.Dispose();
}
好吧,你们是怎么说的?
此外,你们中的一些人提到“内部将以某种方式实现”,您如何看待某些东西的内部实现?
例如,当我浏览 MSDN 中的“泛型概念”时,他们说 List 是泛型的,并展示了如何在内部实现列表的示例实现 here .我们如何才能真正为其他人做到这一点?
我在 VS 2010 中尝试过。但是对象浏览器仅显示特定类型公开的方法或字段。
(我只是很好奇,因为 list 的内部实现的例子太棒了。非常有趣的东西!)
最佳答案
C# specification在第 8.13 章对此进行了解释:
A using statement of the form
using (ResourceType resource = expression) statement
corresponds to one of three possible expansions. When ResourceType is a non-nullable value type, the expansion is
{ ResourceType resource = expression; try { statement; } finally { ((IDisposable)resource).Dispose(); } }
Otherwise, when ResourceType is a nullable value type or a reference type other than dynamic, the expansion is
{ ResourceType resource = expression; try { statement; } finally { if (resource != null) ((IDisposable)resource).Dispose(); } }
Otherwise, when ResourceType is dynamic, the expansion is
{ ResourceType resource = expression; IDisposable d = (IDisposable)resource; try { statement; } finally { if (d != null) d.Dispose(); } }
关于您的第二个问题:.NET 源代码可用于浏览器/下载。检查这个问题:Downloadable/browsable version of the .NET Framework source code?
关于c# - 在 C# 中查看类型 T 的内部实现的可能性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18629056/