CSharp examples for Custom Type:Property
Check property setter value
using static System.Console; using System;//from w w w . j a va2s .co m using System.Collections.Generic; class Program { static void Main(string[] args) { var sam = new Person { Name = "Sam", DateOfBirth = new DateTime(2020, 1, 27) }; sam.FavoriteIceCream = "Chocolate"; WriteLine($"Sam's favorite ice-cream flavor is {sam.FavoriteIceCream}."); sam.FavoritePrimaryColor = "Red"; WriteLine($"Sam's favorite primary color is {sam.FavoritePrimaryColor}."); } } public class Person : object { public string Name; public DateTime DateOfBirth; public List<Person> Children = new List<Person>(); public readonly DateTime Instantiated; public const string Species = "Programmer"; public readonly string HomePlanet = "Earth"; public Person() { Name = "Unknown"; Instantiated = DateTime.Now; } public string FavoriteIceCream { get; set; } // auto-syntax private string favoritePrimaryColor; public string FavoritePrimaryColor { get { return favoritePrimaryColor; } set { switch (value.ToLower()) { case "red": case "green": case "blue": favoritePrimaryColor = value; break; default: throw new System.ArgumentException($"{value} is not a primary color.Choose from: red, green, blue."); } } } }