Refactoring - OOP Example from VB.NET to C#

OK, I don't want to leave out the C# developers from this VB.NET OOP example that Raymond posted recently. So here is one way to accomplish this code in C#.

 

Inheritence, Interfaces, and OOP (oh my!)
public class foo { public static Int32 GetNumberOfRequiredTokens(IPerson person) { return person.GetNumberOfRequiredTokens(); } } public interface IPerson { Int32 GetNumberOfRequiredTokens(); } public abstract class Person : IPerson { public Person() { } private Int32 baseTokenAmount = 1; protected Int32 Tokens { get { return baseTokenAmount; } } public abstract Int32 GetNumberOfRequiredTokens(); } public class Child : Person { public override Int32 GetNumberOfRequiredTokens() { return base.Tokens; } } public class Adult : Person { public override Int32 GetNumberOfRequiredTokens() { return base.Tokens * 3; } } public class Infant : Person { public override Int32 GetNumberOfRequiredTokens() { throw new TooYoungException(); } } public class TooYoungException : Exception { } 
Tweet Post Share Update RSS

Hi, I'm John Papa. I author this blog, create courses for Pluralsight and am a Google Developer Expert and Microsoft Regional Director. I travel speaking at events and train technology thought leaders

Tags:
All