CSharp examples for Custom Type:base
Call constructor from super class via base()
using System;//from w w w . ja v a 2 s .c o m public class Account { public static int _nextAccountNumber = 1000; public int _accountNumber; public decimal _balance; public Account() : this(0) { } public Account(decimal initialBalance) { _accountNumber = ++_nextAccountNumber; _balance = initialBalance; } public decimal Balance { get { return _balance; } protected set { _balance = value; } } public void Deposit(decimal amount) { if (amount > 0) { Balance += amount; } } public decimal Withdraw(decimal withdrawal) { if (Balance <= withdrawal) { withdrawal = Balance; } Balance -= withdrawal; return withdrawal; } public string ToAccountString() { return String.Format("{0} - {1:C}", _accountNumber, Balance); } } public class InvestmentAccount : Account { public decimal _interestRate; public InvestmentAccount(decimal interestRate) : this(interestRate, 0) { } public InvestmentAccount(decimal interestRate, decimal initial) : base(initial) { this._interestRate = interestRate / 100; } public void AccumulateInterest() { Balance = Balance + (decimal)(Balance * _interestRate); } public string ToInvestmentAccountString() { return String.Format("{0} ({1}%)", ToAccountString(), _interestRate * 100); } } public class Program { public static void DirectDeposit(Account ba, decimal pay) { ba.Deposit(pay); } public static void Main(string[] args) { Account ba = new Account(100); DirectDeposit(ba, 100); Console.WriteLine("Account {0}", ba.ToAccountString()); InvestmentAccount sa = new InvestmentAccount(12.5M); DirectDeposit(sa, 100); sa.AccumulateInterest(); Console.WriteLine("Account {0}", sa.ToInvestmentAccountString()); } }