C# Methods

Methods are simply subroutines that are attached to some object. For instance, a 'car' object might have a method called 'start' that starts the car (of course we could be speaking here either about a simulation, or the code could really start a car if attached to appropriate hardware!).

Methods in C# can be declared using various access modifiers:

can only be called within the class itself

can only be called within the class itself or from within child classes

available to be called from outside the class itself

available to be called within the same assembly but not from other assemblies.

[If you're looking for one-to-one online lessons in C#, contact me]

Of all these, public, protected and private are common to other languages like C++ and Java. internal is peculiar to C#; it means that the method can only be called from within the same assembly. An assembly in C# means basically a .exe file or a .dll file. Since you can build a program in C# from multiple assemblies (for instance, you can create a user control self-contained in its own assembly which you can then use in other programs), the internal keyword can be used to ensure that a method can only be called within its own assembly.

Let's take a look at some examples of methods. The following code declares a bunch of public methods. There is also a static main method so that the class can be run as a self-contained program.

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

namespace ConsoleApplication1
    class Program
        // Constructor -- called automatically
        // when the class is constructed.
            Console.WriteLine("Constructor called!");

        // A "method" accepting no parameters
        public void Speak()
            Console.WriteLine("Speak method called!");

        // A method that prints (displays) the text passed 
        // to it as a string.
        public void Print(String text)

        // A method that returns a greeting in
        // string form.
        public String GetGreeting()
            return "Greetings!";

        // Main method: called automatically
        // by the operating system.
        static void Main(string[] args)
            // Create a new program object.
            Program program = new Program();

            // Call the 'speak' method

            // Call the 'print' method
            program.Print("Hello there!");

            // Call 'GetGreeting' and display the return value.

            // Wait for a key press before closing
            // the console window

Actually since all these methods are being called from within one class, we could have declared them protected, private or internal instead of public. These access modifiers only make a difference when other classes are involved.

Alongside the access modifiers, you can also chose to declare a method static. This means that it can (only) be called via the class itself, not via objects of that class as would otherwise be the case. Calling a static method is much like calling an instance method, except that you write the name of the class before the dot, instead of a variable containing an object of the class; e.g. Program.display() rather than program.display(), where Program is a class and the program variable contains an object of type Program.