C#
  Home arrow C# arrow Page 3 - C# Methods, part 1
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# Methods, part 1
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 35
    2005-04-26

    Table of Contents:
  • C# Methods, part 1
  • Method Signature
  • Instance Methods
  • Static Methods
  • Methods and StackFrame
  • Methods Overloading

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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).

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT