惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
Hugging Face - Blog
Hugging Face - Blog
月光博客
月光博客
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
罗磊的独立博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
L
LINUX DO - 最新话题
T
Threatpost
Cisco Talos Blog
Cisco Talos Blog
The GitHub Blog
The GitHub Blog
V
V2EX
SecWiki News
SecWiki News
P
Privacy & Cybersecurity Law Blog
Forbes - Security
Forbes - Security
T
Troy Hunt's Blog
S
Security @ Cisco Blogs
Martin Fowler
Martin Fowler
Attack and Defense Labs
Attack and Defense Labs
A
Arctic Wolf
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Register - Security
The Register - Security
Blog — PlanetScale
Blog — PlanetScale
The Last Watchdog
The Last Watchdog
T
Tor Project blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
P
Proofpoint News Feed
O
OpenAI News
Hacker News - Newest:
Hacker News - Newest: "LLM"
小众软件
小众软件
雷峰网
雷峰网
H
Heimdal Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
Engineering at Meta
Engineering at Meta
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
C
Cyber Attacks, Cyber Crime and Cyber Security
Webroot Blog
Webroot Blog
C
Check Point Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
人人都是产品经理
人人都是产品经理
Hacker News: Ask HN
Hacker News: Ask HN
The Hacker News
The Hacker News
V
Vulnerabilities – Threatpost
Microsoft Security Blog
Microsoft Security Blog

博客园 - Justin Shen

[.Net 4.0]泛型的协变,以及高阶函数对泛型的影响 Part 1 随笔 - WCF and Data Service - Justin Shen 面向对象语言中的Environment和Binding 元数据 有多少东西需要学习? [电子书下载]Dissecting a C# Application: Inside SharpDevelop [MSDNTV]Don Box再次出击 :) 追踪你的时间使用状况 听说《csdn开发高手》停刊了 在nant中改变编译的目标平台 让使用MSN就像访问网页一样容易! System.String是不可变的字符串吗? 对中国计算机大学教育的一些牢骚 [VS2005]做了两个Code Expansion用的Code Snippet。 VC2005中依然没有Refactoring和Code Expansion. [VS2005 Tip]定制Code Expansion。 [C++/CLI] 析构函数等于IDisposable::Dispose()方法 用80386汇编来编写asp.net页面。 关于C#泛型中的new()约束
[C++/CLI]在栈上声明Reference Type
Justin Shen · 2004-10-16 · via 博客园 - Justin Shen

上次介绍显示释放资源的文章,我提到了C++/CLI中提供了一种新的对象实例化语法,就仿佛在native c++中在静态栈上定义一个对象一样。今天就让我们来考察一下这个特性。另外,因为在上个帖子里回答Ninputer的一个问题时,犯了一个错误,我在这里纠正一下...

定义以下以一个类。

我们来看一下生成的IL代码
.method assembly static void  foo() cil managed
{
  // Code size       29 (0x1d)
  .maxstack  1
  .locals init ([0] class Test V_0)
  IL_0000:  ldstr      "Hello"
  IL_0005:  newobj     instance void Test::.ctor(string)
  IL_000a:  stloc.0
  IL_000b:  ldloc.0
  IL_000c:  call       instance void Test::Hello()
  IL_0011:  ldloc.0
  IL_0012:  call       instance string Test::get_Message()
  IL_0017:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_001c:  ret
} // end of method 'Global Functions'::foo

如果我们把上面的foo()函数改成gcnew的形式:

然后观察生成的IL代码:

.method assembly static void  foo() cil managed
{
  // Code size       29 (0x1d)
  .maxstack  1
  .locals init ([0] class Test V_0)
  IL_0000:  ldstr      "Hello"
  IL_0005:  newobj     instance void Test::.ctor(string)
  IL_000a:  stloc.0
  IL_000b:  ldloc.0
  IL_000c:  call       instance void Test::Hello()
  IL_0011:  ldloc.0
  IL_0012:  call       instance string Test::get_Message()
  IL_0017:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_001c:  ret
} // end of method 'Global Functions'::foo

可以看到生成的IL代码与前面的是完全一致的。由此可见,这种新的语法只是编译器提供的一种机制,并不是真的在Stack上实例化对象。那么为什么要提供这种语法呢?或者说两种的区别在哪里呢?那就是在上一篇文章中所提到的显式释放资源。只有用Stack方法来定义变量,才能获得自动调用Dispose()方法的好处。

或许,我们今后应该尽量少用gcnew,除非你想手动控制Dispose()方法调用的时机。唯一比较遗憾的是,由于是从vc 2005 tool refreshes才开始支持这种语法,所以vc express的IDE中的intellisense功能还没有跟上,使用“点”操作符时,没有任何提示,不过相信在beta2里一定会全面支持的。另外MS的员工还透露说beta2中,STL.NET可能就会现身了,真是非常期待。

另:

昨天Ninputer问我Stack定义的对象,是用->访问成员还是用“点”来访问成员。我回答是用->来访问,其实应该是“点”才对。前几天试验的时候不知道为什么编译器没有报错......可惜代码已经不在了,也没法知道当时犯了什么错 今天写这个帖的时候才发现不对了...