C#自动实现的属性 [英] c# auto implemented property

查看:84
本文介绍了C#自动实现的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目在使用自动实现的属性时似乎无法正常运行.

使用get/set时,它返回null.但是,当明确告诉它要返回什么时,它就很好用.例如,我只需要更改此内容:

I have a project that does not seem to behave properly when using auto implemented properties.

When using get/set, it returns a null. But when explicitly telling it what to return it works great. For example, I only need to change this:

public string Symbol { get; set; }



对此:



to this:

public string Symbol
{
    get { return _symbol; }
    set { _symbol = value; }
}




而且效果很好.




And it works great.

Does anyone have an explanation?

推荐答案

您是否定义了string _symbol?如果这样做,请删除它,然后查看它是否有效.您可能在那里发生了碰撞(但是,这并不能说明您是否真的为空).

基本上,只要确定您是否在使用快捷方式,就不要声明私有成员.

干杯.
Do you have string _symbol defined? If you do, remove it and see if it works. You might have a collision there (though, that doesn''t explain if you''re actually getting null).

Basically, just make sure if you''re using the shortcut that you''re not also declaring the private member.

Cheers.


没有解释,但我只是尝试了一下,而且自动实现版本也没有问题.
No explanation but I just tried it and I had no problem with the auto implemented version.


也许使用反射器 [ ^ ]并查看编译为自动属性时生成的代码?

干杯
Uwe
Maybe it helps to use Reflector[^] and take a look at the generated code when being compiled as an automatic property?

Cheers
Uwe


这篇关于C#自动实现的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆