static methods in C-Sharp / C#

A method in C# can be defined as static.The static method is class level method i.e. It will be accessible via class not via object of a class.
Let’s update the SavingsAccount class to define two static methods. The first static method
(GetInterestRate()) will return the current interest rate, while the second static method
(SetInterestRate()) will allow you to change the interest rate:


Code Snippet
  1. // A simple savings account class.
  2. class SavingsAccount
  3. {
  4.   // Instance-level data.
  5.   public double currBalance;
  7.   // A static point of data.
  8.   public static double currInterestRate = 0.04;
  10.   public SavingsAccount(double balance)
  11.   {
  12.     currBalance = balance;
  13.   }
  14.   // Static members to get/set interest rate.
  15.   public static void SetInterestRate(double newRate)
  16.   { currInterestRate = newRate; }
  18.   public static double GetInterestRate()
  19.   return currInterestRate; }
  20. }

Now, observe the following usage:

Code Snippet
  1. static void Main(string[] args)
  2. {
  3.   Console.WriteLine("***** Fun with Static Data *****\n");
  4.   SavingsAccount s1 = new SavingsAccount(50);
  5.   SavingsAccount s2 = new SavingsAccount(100);
  7.   // Print the current interest rate.
  8.   Console.WriteLine("Interest Rate is: {0}", SavingsAccount.GetInterestRate());
  10.   // Make new object, this does NOT 'reset' the interest rate.
  11.   SavingsAccount s3 = new SavingsAccount(10000.75);
  12.   Console.WriteLine("Interest Rate is: {0}", SavingsAccount.GetInterestRate());
  14.   Console.ReadLine();
  15. }

The output of the previous Main() is seen here:
***** Fun with Static Data *****
Interest Rate is: 0.04
Interest Rate is: 0.04 

As you can see, when you create new instances of the SavingsAccount class, the value of the static
data is not reset, as the CLR will allocate the static data into memory exactly one time.
After that point, all objects of type SavingsAccount operate on the same value for the static currInterestRate field.
When designing any C# class, one of your design challenges is to determine which pieces of data
should be defined as static members, and which should not.
While there are no hard and fast rules, remember that a static data field is shared by all objects of that type. Therefore, if you are defining a point of data that all objects should share between them, static is the way to go.

Consider what would happen if the interest rate variable were not defined using the static keyword.
This would mean every SavingsAccount object would have its own copy of the currInterestRate field.
Now, assume you created 100 SavingsAccount objects, and need to change the interest rate. That would
require you to call the SetInterestRate() method 100 times! Clearly, this would not be a very useful way
to model “shared data.”  Again, static data is perfect when you have a value that should be common to all
objects of that category.

Post a Comment