0

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.

3
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by Mike Askew
1

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");
}
1

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);
}

Edited by nakor77: modified default value

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.