#238 – Call a Method from Another Method
February 10, 2011 Leave a comment
You can call an instance method of a class from another instance method within that class. The second method called will execute upon the same instance of the class for which the first method was called.
In the example below, we can call the Dog.Growl method, or the Dog.BarkAndGrowl method which in turn calls Growl.
public class Dog { public string Name; public int Age; public void BarkAndGrowl() { Console.WriteLine("Woof. --{0}", Name); Growl(); } public void Growl() { Console.WriteLine("Grrr. --{0}", Name); } }
Here’s an example of calling these methods on an instance of the Dog class.
Dog kirby = new Dog(); kirby.Name = "Kirby"; kirby.Growl(); // Grrr. kirby.BarkAndGrowl(); // Woof. Grrr.