Wednesday Notes and Exercises

Today, we’re going to focus on methods.

All objects have properties and methods.

Properties are attributes of the object.  For example, if you had a cat object, its fur color would be a property.  The color of its eyes would be a property.  Whether it was hungry or not would be a property.

Methods are the actions of an object.  In the cat example, a cat might have a meow method, or a purr method, or a scratch method.

When we are reading or creating code, we need to be able to identify methods so that we can have reasonable expectations as to what they do.  We should be able to look at the method’s signature and easily see what the method returns, the name of the method, and what parameters can be passed in to a method.

 

In a simple method signature, there are four parts, in the following order:

ProtectionLevel ReturnType Name(Parameters)

There are several options for ProtectionLevel.  We will mostly focus on two in this club: private and public

 

ReturnType can be any object type.  It could be a string, an int, a bool… it can be any object that we create.  If a method does not return an object, the ReturnType should be void.

The method name is the name of the method.  It can be nearly anything.

The parameters are the objects that can be passed in to the method.  Parameters include the type of object and the name of the object being passed in.  Parameters are separated by commas.

 

Method Examples:

private string GetName()
 {
 return "Brian";
 }
public void Meow()
 {
 Console.Out.WriteLine("Meow!");
 }
private int AddTen(int someNumber)
 {
 return someNumber + 10;
 }
public Card DealACard()
 {
 return new Card();
 }
public List<Card> DealAHand()
 {
 List<Card> cardList = new List<Card>();
 return cardList;
 }
private int AddTwoNumbers(int firstNumber, int secondNumber)
 {
 return firstNumber + secondNumber;
 }

One comment on “Wednesday Notes and Exercises

  • I’ve missed a few days so I know that I am behind. Should I just reffer to the notes. I gave some thought to dropping out but I want to be here. It’s just that I have a lot going on right now and it’s hard to stay on track.

Leave a Reply

Your email address will not be published. Required fields are marked *