#232 – Declaring and Using Instance Methods in a Class

In a class, an instance method is a kind of class member that is a block of statements that will execute when a user of the class calls the method.  It typically acts upon the data stored in that particular instance of the class.

A method has a return value that allows the method to return a result, or a return type of void, indicating that it doesn’t return any result.

Here’s a simple example of a method:

    public class Dog
    {
        public string Name;
        public int Age;

        public void BarkYourAge()
        {
            for (int i = 1; i <= Age; i++)
                Console.WriteLine("Woof");
        }
    }

Calling our method:

    Dog rover = new Dog();
    rover.Name = "Rover";
    rover.Age = 3;

    rover.BarkYourAge();

The result:

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

One Response to #232 – Declaring and Using Instance Methods in a Class

  1. Pingback: #281 – Declaring and Using Static Methods in a Class « 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers