Chapter 8: Methods.

Through this book we've scene a lot of examples of methods, we used WriteLine and ReadLine from the Console class and Next from the Random class, and we in fact wrote one in the previous chapter, the constructor is a kind of method.

so, let's see how can we create methods.

using System;

public class Player
{
    public int x;
    public int y;
    public int z;
    publicPlayer(int x, int y, int z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    public void SayPosition()
    {
        Console.WriteLine($"{x}, {y}, {z}");
    }
}

methods contain 4 parts

  • Return type, it can be an int, string, or any other type, if we won't return anything, we use the void type, which is nothing.
  • Identifier, holds the same rules as variables, can contain numbers and characters and an underscore, but can't begin with a number.
  • argument list, wrapped between 2 parens, can be empty if we have no arguments.
  • The body, which is basically a block, this belongs to this method and when the method is called, that code gets executed.

the constructor is a special method because there's no need to give it a return type, because it automatically deals with that itself.

the using System; part pulls the System namespace, namespaces are like folders that contain classes, and Console is under the System namespace.

We don't do that in our Program.cs because Program.cs automatically pulls System, but since we're putting all our classes in a different place we manually need to pull it.

lets try using our class from above.

Player player = new(0, 2, 1);
player.SayPosition();

now if you read our code carefully you'll notice that, unlike Console.WriteLine and ReadLine, we must create an instance first of the Player class before being able to call SayPosition, this has a few reasons.

  1. The SayPosition is an instance method,not a static method, instance methods need to be called from an instance.
  2. Even if we made it static we need to change it first, because there won't be x y z because we haven't created any instance so x y z doesn't exist.

let's look at an example of how can we make static methods.

using System;

public class Player
{
    public static SayPosition(int x, int y, int z)
    {
        Console.WriteLine($"{x}, {y}, {z}");
    }
}

and we try it, again remember, classes go to separate files and code that we want to run goes to Program.cs.

Player.SayPosition(0, 3, 0);

you also can make variables static.

using System;

public class Player
{
    public static int x =0;
    public static int y = 4;
    public static int z = 0;
    public static SayPosition()
    {
        Console.WriteLine($"{x}, {y}, {z}");
    }
}

The problem with static variables, though, is that they are like normal variables. You only get one copy, all instances of the class will use the same variable, which will be a problem.

using System;

public class Player
{
    public static string name = "john";
    public int x;
    public int y;
    public int z;
    public Player(int x, int y, int z, string playerName)
    {
        this.x = x;
        this.y = y;
        this.z = z;
        name = playerName;
    }
    public void Speak()
    {
        Console.WriteLine($"Hello, I'm {name} and I'm at  {x}, {y}, {z}");
    }
}

Now let's try using that.

Player player1 = new(0, 3, 0, "ahmad");
Player player2 = new(0, 6, 1, "oscar");
player1.Speak();
player2.Speak();

You'll notice that both show oscar. This is because name is static, there's only one piece of it, it has nothing to do with the instance, so even though you created one, it's still one variable, it's like the normal variables we create anywhere, but it's inside a class and doesn't need an instance.

now, there's one part of methods that we didn't cover befoe.

Remember with Console.ReadLine we got a string from it? We can also do that with our custom methods.

using System;

public class Player
{
    public static string Hello()
    {
        return "Hello, World!";
    }
}

return is like a break; in loops but it completely stops the function, use return anywhere, the function is finished.

and it's also used to give back values, like what we did, remember that we can only return a value of the type that we spesified in the method definition, if we had string, we can't do int, and vise versa.

lets try using our method.

Console.WriteLine(Player.Hello());

Next, inheritence.