Say I have the below example, the syntax is based off constuctor chaining for clarity (and my cluelessness).

    public static void Feed() : this("Unknown"){}

    public static void Feed(string FoodType)
    {
        if (Food != "Unknown")
            Console.WriteLine("{0} eats some {1}.", Name, Food);
    }

How would I actually implement this for methods? It is of the same style as constructor chaining, that if nothing is passed to the Feed method it will pass Unknown to the latter Feed method.

Such a simple question but its bugging me lol.

Recommended Answers

All 3 Replies

If Feed is a method, it would look something like this:

public void Feed(string FoodType)
{
    if (Food != "Unknown")
        Console.WriteLine("{0} eats some {1}.", Name, Food);
}

public void Feed()
{
    Feed("Unknown");
}

If you're using .net 4.0 or later you can create default values for your parameters

public static void Feed(string Food="Unknown")
{
    if (Food != "Unknown")
    Console.WriteLine("{0} eats some {1}.", Name, Food);
}

Thank you both, learn something new everyday :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.