A static method in a class is similar to an instance method, except that it acts upon the class’ static data–fields and properties–rather than on the instance data stored with a single instance of the class.
There is only one copy of each static data item, no matter how many instances of the class exist.
Here’s an example of defining a static method in a class.
1
2
3
4
5
6
7
8
9
| // Static property -- one value for all dogspublic static string Creed { get; set; }// Static method acts on static datapublic static void RepeatYourCreed(int numRepeats){ for (int i = 0; i < numRepeats; i++) Console.WriteLine("My creed is: {0}", Creed);} |
To call a static method, you just prefix the method with the name of the class (rather than with a reference to an instance of the class).
1
2
3
4
5
| // Set static propertyDog.Creed = "We serve man";// Call static methodDog.RepeatYourCreed(3); |

