3

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?

user366312
  • 16,949
  • 65
  • 235
  • 452

2 Answers2

8

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.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
0

Answer: You can't do this with automatic properties.

recursive
  • 83,943
  • 34
  • 151
  • 241