using System;
namespace MyNameSpace
{
public class Foo
{
public Foo()
{
x = 0;
}
private int x;
public int X
{
get
{
return x;
}
set
{
if (value > 0)
{
x = value;
}
else { x = 0; }
}
}
static void Main(string[] args)
{
Foo f = new Foo();
f.X = 7;
Console.WriteLine("x = {0}", f.X);
f.X = -3;
Console.WriteLine("x = {0}", f.X);
}
}
This code will output
x = 7
x = 0