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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

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