C#3.0具有额外逻辑的自动属性 [英] C#3.0 Automatic properties with extra logic

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

问题描述

如何使用C#3.0自动属性重写以下代码?

How can I rewrite he following code using C#3.0 automatic properties?

private int _myValue;
        public int MyProperty 
        {
            get { return _myValue;}
            set
            {
                if (value > 0)
                {
                    _myValue = value;
                }
            }
        }

如果不可能,还有什么选择?

If it is not possible, What is the alternative?

推荐答案

否,自动实现的属性没有声明的实现.您希望提供的任何扩展实现都必须使用常规属性.

No, automatically implemented properties have no declared implementation. Any extended implementation that you wish to provide would have to use a regular property.

我不确定您要寻找的是替代方案-您在问题中使用的语法是替代方案.

I am not sure what you are looking for in terms of an alternative - the syntax you have used in your question is the alternative.

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

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