public class Person { private string _name; public string Name { get { return _name; } set { _name = value; } } }
public class Circle { private double _radius; public double Radius { get { return _radius; } set { if(value > 0) { _radius = value; } else { throw new Exception("Radius cannot be negative."); } } } public double GetArea() { return Math.PI * (_radius * _radius); } }This example shows a property for the Radius field in a Circle class. The set method includes an additional check to ensure that the value passed in is not negative. The GetArea method calculates the area of the circle using the stored radius value. These examples use the System package library in C#.