C# Methods, part 1 - Instance Methods
(Page 3 of 6 )
The different types of methods come from the fact that we have a class and an object of a class. The expression "Instance Method" means that it's a method of an object, and this method will only work on that instance object (not the class). An instance method has direct access to all data inside the instance object, and can modify only this object's data (fields). It has no access to other instance's data and can't affect it directly, so we can say that instance methods have object scope -- this means that they can use the keyword this to access other members of the same instance.
As we said before, you can create instance methods inside C# structures and still use the this keyword. It may not make sense to you, because you might think that the this keyword is a reference to an instance object, so it has an access only for Reference-Types. But when you use it with a structure (Value-Type), it will behave differently. When you use the this keyword on a structure to call an instance method, the value itself will be copied to the stack, and we will have access to this value using the this keyword.
It makes sense now because you are referring to THIS value, inside the method, and the method now can work on the value itself. When the method's execution is complete, the new value will be copied back to its location. This is the way the C# this keyword works, and makes sense with both Reference-Types and Value-Types. Now let's look at an example of instance methods.
using System;
namespace MyCompany
{
public class Methods
{
static void Main(string[] args)
{
Employee Michael = new Employee("Michael", "Youssef", 7000m, Position.Engineer);
Console.WriteLine(Michael.MonthlySalary);
Michael.RaiseSalary();
// another Employee
Employee Steven = new Employee("Steven", "Peter", 9000m, Position.Manager);
Console.WriteLine(Steven.MonthlySalary);
Steven.RaiseSalary();
Console.ReadLine();
}
}
public class Employee
{
public string FirstName;
public string LastName;
public decimal MonthlySalary;
public Position EmpPosition;
public Employee(string FN, string LN, decimal MS, Position EP)
{
this.FirstName = FN;
this.LastName = LN;
this.MonthlySalary = MS;
this.EmpPosition = EP;
}
public void RaiseSalary()
{
switch(this.EmpPosition)
{
case Position.Accountant:
Console.WriteLine("The employee salary will be raised 10%");
this.MonthlySalary = this.MonthlySalary * 1.10m;
Console.WriteLine("The employee salary now = " + this.MonthlySalary);
break;
case Position.Lawyer:
Console.WriteLine("The employee salary will be raised 15%");
this.MonthlySalary = this.MonthlySalary * 1.15m;
Console.WriteLine("The employee salary now = " + this.MonthlySalary);
break;
case Position.Engineer:
Console.WriteLine("The employee salary will be raised 20%");
this.MonthlySalary = this.MonthlySalary * 1.20m;
Console.WriteLine("The employee salary now = " + this.MonthlySalary);
break;
case Position.Manager:
Console.WriteLine("The employee salary will be raised 25%");
this.MonthlySalary = this.MonthlySalary * 1.25m;
Console.WriteLine("The employee salary now = " + this.MonthlySalary);
break;
}
}
}
public enum Position
{
Accountant = 1,
Lawyer,
Engineer,
Manager
}
}
Compile the code then run it and you will get the following result:

The code contains the Employee class, the Position enumeration and the Methods class, which contains the Main method. Note that we called the instance method RaiseSalary() using the object, not the class, so in the code we called it twice: once on the Michael object, and another time on the Steven object, to raise their salaries.
Note the use of the Position enumeration as a data type for the field MonthlySalary, and note the use of a switch/case statement in the method RaiseSalary() to indicate the Employee position and raise his/her salary based on that. Also note that we have used the keyword this to refer to the current instance.
Methods process data in one of two ways. The first way is to work on the class fields, like our RaiseSalary() method which works internally on the MonthlySalary field. The second way is to pass values to methods. Methods like Next(int minValue, int maxValue) are called parameterized methods, because they have parameters for the values they work on (process). The value that you pass to the method called argument and the place holder of that value (that declares its type and name) is called a parameter.
In the second part of this article we will discuss Passing arguments by value and by reference. Note that many programmers call the argument a parameter and vice versa. Most commonly we will call them parameters and parameter values (for the argument).
Next: Static Methods >>
More C# Articles
More By Michael Youssef