Skip to content

Instantly share code, notes, and snippets.

@FredrikAugust
Created March 4, 2015 17:25
Show Gist options
  • Save FredrikAugust/7ecba886e44750397c7c to your computer and use it in GitHub Desktop.
Save FredrikAugust/7ecba886e44750397c7c to your computer and use it in GitHub Desktop.
C# Interface Super-Basic
using System;
namespace PersonPrinter
{
interface IPerson
{
string Name { get; set; }
int Age { get; set; }
}
class Person : IPerson
{
private string name;
private int age;
public string Name
{
get
{
return this.name;
}
set
{
if (value.Length > 1)
{
this.name = value;
}
else
{
throw new Exception("Invalid name, try again.");
}
}
}
public int Age
{
get
{
return this.age;
}
set
{
try
{
this.age = Convert.ToInt32(value);
}
catch (Exception)
{
throw;
}
}
}
}
public class Program {
public static void Main(string[] args)
{
Person TestChar = new Person();
Console.WriteLine("What do you want to call your person:");
string name = Console.ReadLine();
TestChar.Name = name;
Console.WriteLine("How old is {0}?", TestChar.Name);
string ageString = Console.ReadLine();
TestChar.Age = Convert.ToInt32(ageString);
Console.WriteLine("{0} is {1} years old.", TestChar.Name, TestChar.Age);
Console.WriteLine("\nPress any key to exit..");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment