Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

c# 3.0 - C#3.0 Automatic properties with extra logic

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...