Lesson 6: Functions/Methods

In the previous lessons, you have been putting your code in the “Main”. The main is a function (AKA methods). MethodsĀ is a group of statements and code that perform a givenĀ task. This is useful because you can use a method multiple times without rewriting the code. To use a method you have to define a method, and you have to call (run) the method latter on.

In C# the method structure is similar to Java.

<Access Modifier> <Return Type> <Method Name>(Parameter List)
{
   Method Body
}

An access modifier determines the visibility and accessibility of a method, class, variable, etc. in other classes and methods. This is like setting permissions of the method, and whether other classes can use this method. This is useful for separating classes and methods from interfering with each other, and is a key component of encapsulation and object-oriented programming in general. Here is a chart explaining the different access modifiers, in C#, C++ and Java.

Keyword C# C++ Java
private class class class
protected internal same assembly and
derived classes
protected derived classes derived classes derived classes
and/or
within same package
package within its package
public internal same assembly
public everybody everybody everybody

The return type specifies the type of the information returned, if there is information to be returned to where the function is called. The return types are usually the the type of the information return (eg. int if ints are returned). Void is used if no information is returned.

The method name is the name of the method. Pretty straight forward. (Warwick is OP btw)

Lastly there is the parameter list. The parameter list is used to pass information to the method. When an method is called, and parameters are passed, the method can use these parameters and process them accordingly. Parameters are what is given to the method, and the when the parameters are used in a method they are called arguments.

Sometimes the keyword static is used. This just means that there is only 1 instance of the code, and can be run as is, instead of being created in a new object first.
Here is a simple method, where it is called latter on in the code. :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace methods
{
    class Program
    {
        public static void myMethod(string stuff)
        {
            Console.WriteLine("myMethod prints the parameter stuff. Here it is: {0}", stuff);
        }
        static void Main(string[] args)
        {
            //We will now run myMethod with the argument Draven
            myMethod("Draven");
            Console.ReadKey();
            //the output:
            //myMethod prints the parameter stuff. Here it is: Draven
        }

    }
}

A neat thing that you should know that is awesome is that you can pass references in a method in C#. This means that you can pass a reference to an object, and the method will execute command as if it is on the other object accordingly. So it will pass what is directly in the memory at that time, instead of a copy of the variable passed through.

If you need more help, take a look at one of these pages:
http://www.csharp-station.com/Tutorial/CSharp/Lesson05
http://www.tutorialspoint.com/csharp/csharp_methods.htm
http://msdn.microsoft.com/en-us/library/ms173114.aspx

Leave a Reply

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