reptildarat
reptildarat

Reputation: 1036

C# Overloading with Return void and object (string)

I try to create an overloading function that return void or an string like so:

public string Message { get; private set; }

public void Foo (Bar bar)
{
    Message = "Hello World!";
}

public string Foo (Bar bar)
{
    return "Hello World!";
}

But I got Compile Time Error says:

 Type 'Foo' already defines a member called with the same   parameter types.

Is there any way that I can achieve that sort of overload?

Upvotes: 3

Views: 671

Answers (2)

Shrivallabh
Shrivallabh

Reputation: 2893

No you can't do that. Return type change is not the method signature change. Here is the link talks about guidelines of Overloading

Upvotes: 1

nvoigt
nvoigt

Reputation: 77304

No, there is no way to have such an overload.

What method would the compiler call on this line:

Foo (new Bar());

It would be perfectly valid syntax for both with no way to distinguish and that must not happen.

Method overloading must have different parameters. Just different return types is not enough.

Upvotes: 6

Related Questions