An interesting difference between C# and C++/CLI

Considering code below:

// Component code:
class B
{
 public int Val
 {
  get {return 1;}
 }
}
class D : B
{
 public new int Val
 {
  set {value;}
 }
}

// Client code:
// C#
D d = new D();
int i = d.Val; // compile error
// C++/CLI
D^ d = gcnew D();
int i = d->Val; // ok

Why is there this diversity? As we know, all .net program would output to IL code, so the code and its metadata should be same as each other, but why the get accessor is available in C++/CLI but invalid in C#? In addition, is it valid if from perspective of CLR?

Then I used IL disasembler to get IL of above code and found that B class includes a Val property definition with get accessor, D includes a Val property definition with set accessor, and get and set methods are marked as hide sig respectively. d->Val in C++/CLI is compiled to code like below:

int i = d->B::Val::get()


I guessed C++/CLI compiler try its best to query get method, so it invokes base class's in case of no get method definition in child class's metadata, while C# prefer treating the property as an integrity, so it is invalid when compiler find the property without get accessor hides base on.

The fact that child class doesn't include get definition can be proven by reflection:

// C#
D d = new D();
int i = (int)typeof(B).GetProperty("Val").GetValue(d); //ok
i = (int)typeof(D).GetProperty("Val").GetValue(d); // an exception will be thrown

We can get the same result if using C++/CLI as expected, since reflection is independent with concrete languages.

Conclusion:
As a basic grammar in C# and CLR, property is considered as an elements, which include one or two methods definition  but not equal to methods. However, since standard c++ doesn't define property, C++/CLI designer may prefer treating it as one or two seperate methods. This case demonstrates the different philosophy between two languages in the face of the same issue, interesting!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值