C # Overloading returning void and object (string)
I am trying to create an overload function that returns void or a string like this:
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 the Compile Time
Error:
Type 'Foo' already defines a member called with the same parameter types.
Is there a way that I can achieve such an overload?
+3
source to share
2 answers
No, there is no such overload.
What method will the compiler call on this line:
Foo (new Bar());
It would be perfectly valid syntax for both without being able to distinguish, and it shouldn't be.
Method overloading must have different parameters. It's just that different return types aren't enough.
+6
source to share