The Best C# Programmer In The World - Ben Perkins Member Login  | Newsletter | RSS Feeds


 
 
 
TheBestCSharpProgrammerInTheWorld.com 
 
The Best C# Programmer In The World - Ben Perkins
Interface in C#  
 
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 Catagorty 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 impements 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()
                                {
                                Console.WriteLine("Account Characteristics");
                                Console.WriteLine("accountNumber: {0}", accountNumber);
                                Console.WriteLine("accountLogin: {0}", accountLogin);
                                Console.WriteLine("accountBalance: {0}", accountBalance);
                                Console.WriteLine("lastLogin: {0}", lastLogin);
                                Console.WriteLine("AccountCategory: {0}", accountType);
                                Console.WriteLine("minBalance: {0}", minBalance);
                                Console.WriteLine("currency: {0}", currency);
                                Console.WriteLine("advisorEnabled: {0}", 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 intances 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 guidlines for your development and business models
 
Download the source
 
 
 
Feedback / Question
 
Your Name:Your Email:
 
Subject:
 
Feedback/Question:
 
 
 
I had to remove the capability to leave feedback due to this. Will be back soon.
 
 
Comment posted: 12/21/2010 4:15:56 AM by Himber
 
I have always liked the concept of Interfaces. But have had a hard time finding a way to apply them, you article helped with this. Many thanks!
 
page.Translate()
 
 
blog.Stats()
 
  Posts: 113
  Comments: 86
  Fundamentals: 16
 
my.Book()

 
me.About()
 
 
 
 
 
blog.Archive()
 
2012 May  (4)
2012 April  (5)
2012 March  (4)
2012 February  (4)
2012 January  (5)
2011 December (2)
2011 November (6)
2011 October (7)
2011 September (7)
2011 August (9)
2011 July (9)
2011 June (8)
2011 May (9)
2011 April (7)
2011 March (9)
2011 February (8)
2011 January (8)
2010 December (7)
2010 November (8)
2010 October (4)
 
site.Visits()
 
free counters
 
tag.Cloud()
 
code.Disclaimer()
 
The sample code on this website is provided to illustrate a concept and should not be used in applications or Web sites without proper professional consultation, as it may not illustrate the safest coding practices. I assume no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
 
   


The Best C# Programmer In The World - Ben Perkins, © 2010, All Rights ReservedContact Ben