Interface in C#

An interface is like a contract. When a person signs a contract they are agreeing to perform in adherence to its’ contents. The person can take any action which is not specifically identified in the contract or build on top of the agreements within it. An interface has a similar concept. In this example I will refer to an IAccount interface. The expectation is that when a programmer implements this interface into thier program that all the contents of the Interface will be implemented. The below interface expects that the class implementing the the interface will have an Account Number, a Account Login, an Account Balance, a Last Login Date/Time and have an Account Type:

public interface IAccount
{
  string accountNumber { get; set; }
  string accountLogin { get; set; }
  float accountBalance { get; set; }
  DateTime? lastLogin { get; set; }
  AccountCategory accountType { get; set; }
  
  void AccountCharacteristics();
}

As well, we expect the class implementing this interface to create a function called AccountCharacteristics(). I didn’t specify exactly what it does, but I made the name intuitive enough for them to figure out what is needed.

The Account Category type is an ENUM and looks like this:

public enum AccountCategory
{
  Checking,
  Savings,
  Retirement,
  Investment
}

I am providing a list of Account Categories that can be used when populating the class with data.

AccountCategory.Savings, AccountCategory.Investment, etc...

In this example I also want to provide an interface that exposes some account dimensions:

public interface IAccountDimensions
{
  float minBalance { get; set; }
  string currency { get; set; }
  bool advisorEnabled { get; set; }
}

So when a class implements this interface they need to create a minimum balance, currency and advisor property. The below example of an Account class implements both the IAccount and IAccountDimensions. It implements all the properties identified in the 2 interfaces, plus the AccountCharacteristics method. In the constructor, I only wanted the Account Number, Account Login, Account Balance and Account Type loaded.

public class Account : IAccount, IAccountDimensions
{
  public string accountNumber { get; set; }
  public string accountLogin { get; set; }
  public float accountBalance { get; set; }
  public DateTime? lastLogin { get; set; }
  public AccountCategory accountType { get; set; }
  public float minBalance { get; set; }
  public string currency { get; set; }
  public bool advisorEnabled { get; set; }

  public Account(string AccountNumber, 
                 string AccountLogin, 
                 float AccountBalance, 
                 AccountCategory AccountType)
  {
    accountNumber = AccountNumber;
    accountLogin = AccountLogin;
    accountBalance = AccountBalance;
    accountType = AccountType;
  }

  public void AccountCharacteristics()
  {
    WriteLine("Account Characteristics");
    WriteLine($"accountNumber: {accountNumber}");
    WriteLine($"accountLogin: {accountLogin}");
    WriteLine($"accountBalance: {accountBalance}");
    WriteLine($"lastLogin: {lastLogin}");
    WriteLine($"AccountCategory: {accountType}");
    WriteLine($"minBalance: {minBalance}");
    WriteLine($"currency: {currency}");
    WriteLine($"advisorEnabled: {advisorEnabled}");
  }
}

When loading the data into your class for processing, I again mention that I do not request/want all the properties loaded when the class gets instantiated. I did this to show that it is also possible to access and modify the properties of the class after the instance has been created, like I show below:

Account savingsAcct = 
    new Account("SS345-9I", "JADFL2", .34f, AccountCategory.Savings);
    savingsAcct.lastLogin = DateTime.Now;
    savingsAcct.minBalance = 5000.00f;
    savingsAcct.currency = "USD";
    savingsAcct.advisorEnabled = false;

The first row creates the instances passing the required parameters to the constructor. Then I can access the other properties using the instance directly.

An interface is a powerful tool that can be used to enforce guidelines for your development and business models

Download the source




Leave a Comment

Your email address will not be published.