Nomnom
Nomnom

Reputation: 4835

Are there optional arguments without default values, that can "not exist" unless passed/specified/used?

I have a method

public void Foo(String someString = "A String")
{
    Console.WriteLine(someString);
}

That is being called within another method

// No arguments unless one is specifically passed when called
public void Bar(String anotherString = )
{
    Foo(anotherString);
}

You can probably already tell what I'm trying to do here, I'd like to only pass a String if a value is given when calling Bar(). If no argument is passed for Bar() I'd like to omit anotherString entirely so that the default values of Foo() are used instead.

Is there a way to do this?

Upvotes: 0

Views: 81

Answers (1)

mm8
mm8

Reputation: 169190

Use an overload:

public void Foo(String someString = "A String")
{
    Console.WriteLine(someString);
}

public void Bar(String anotherString = "a")
{
    Foo(anotherString);
}

public void Bar()
{
    Foo();
}

Upvotes: 3

Related Questions