Some time ago I wrote a homework, concerning building classes. The easiest part of it looked like this as a task:
Define a class Person that has name, age and email. The name and age are mandatory. The email is optional. Define properties that accept non-empty name and age in the range [1…100]. In case of invalid argument, throw an exception. Define a property for the email that accepts either null or non-empty string containing ‘@’. Define two constructors. The first constructor should take name, age and email. The second constructor should take name and age only and call the first constructor. Implement the ToString() method to enable printing persons at the console.
After some time, I managed to create the following main method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; using System.Collections.Generic; using System.Linq; namespace <span class="hiddenSpellError">Homework1</span> { class <span class="hiddenSpellError">PersonsClass</span> { static void Main() { List persons=new List(){ new Person("The Academic Guy", 29), new Person("Me", 29, "notarealaddress@abv.bg"), new Person("Vitya", 29, "notarealaddress@yahoo.com"), new Person("Noname",99, "notarealaddress@yahoo.com"), }; persons.ForEach(myPerson => Console.WriteLine(myPerson.ToString())); } } } |
What it does is the following:
The question is how? 🙂
The answer is quite obvious for anyone dealing with Object Oriented Programming – we create an object and we simply call it with the “ToString” from the LINQ.
Let’s see what we got here.
- We should have a class, named “Person” with three properties – name, age and e-mail.
- The we need 2 constructors – one accepting only name and age and one accepting email, age and name. To make it more interesting, the bigger one inherits the smaller.
- The third thing we should do is to define the get and the set of the three properties.
- At last, we override the ToString method. This means, that in our class ToString would do what we want and not what .Net usually expects from it. In the ToString method there is a checker whether we have E-mail or not.
So, this is how the code looks like! Enjoy it if you can 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
using System; namespace <span class="hiddenSpellError">Homework1</span> { public class Person { private string name; private int age; private string email; public Person(string name, int age, string email) { this.Age = age; this.Name = name; this.Email = email; } public Person(string name, int age) : this(name, age, null) { } public string Name { get { return this.name; } set { if (string.IsNullOrEmpty(value)) throw new <span class="hiddenSpellError">ArgumentException</span>("Invalid name!"); this.name = value; } } public int Age { get { return this.age; } set { if(value<1 || value>100) throw new <span class="hiddenSpellError">ArgumentException</span>("Age is outside the allowed range!"); this.age = value; } } public string Email { get { return this.email; } set { if(null!=value && (value.Length==0 || !value.Contains("@"))) throw new <span class="hiddenSpellError">ArgumentException</span>("Invalid email!"); this.email = value; } } public override string <span class="hiddenSpellError">ToString</span>() { return string.Format("name: {0}, age: {1}", this.Name, this.Age)+(this.Email==null?"":", email: "+this.Email); } } } |
🙂