Is it possible to offer return options in a method?

4

As I already know there is overload in creating the methods, which are example, a method where you have the option of passing 2 or 3 parameters calling the same function:

//Soma 2 numeros
public int  Somar(int a, int b)
{
    return a + b;
}    

//Soma 3 numeros
public int  Somar(int a,int b, int c)
{
    return a + b + c;
}

The question is: Can I create a return overload? For example:

public PessoaJuridica RetornarPessoa(string id)
{
    return new PessoaJuridica();
}

public PessoaFisica RetornarPessoa(string id)
{
    return new PessoaFisica();
}

The need arose because I have two methods with different names and it would be easier for me to use the same name, as they do "the same thing".

    
asked by anonymous 14.08.2017 / 20:24

3 answers

4

Not possible. Signing method does not consider the return type. If it were possible, it would complicate the compiler's work to know which method you want to use.

I do not quite understand how you intend to do this and there are not enough details for me to understand, but I imagine you can solve this using a generic method.

public static T RetornarPessoa<T>(string id) where T: new()
{
    return new T();
}

I suppose that you have some hierarchy in the classes. So I gave this example of how my idea can be implemented:

See it working on .NET Fiddle.

public class Program
{
    public static void Main()
    {
        var pessoa = RetornarPessoa<PessoaFisica>("a550");
        Console.WriteLine(pessoa.Id);
    }

    public static T RetornarPessoa<T>(string id) where T: Pessoa, new()
    {

        return new T { Id = id };
    }
}

public abstract class Pessoa
{       
    public string Id { get; set; }
}

public class PessoaJuridica : Pessoa
{
    public string RazaoSocial { get; set; }
}

public class PessoaFisica : Pessoa
{
    public string NomeCompleto { get; set; }
}
    
14.08.2017 / 20:28
4

This is not possible, just method signature consider its name and parameter types.

If you did the return would complicate the compiler, it would create ambiguity and possible bugs .

The most sensible solution in this case is to place a differential in the method name indicating the type of feedback it will provide. It gets even more readable. I've come to think of a few others, but nothing that is recommended. I even started a response with an alternative, but it is something that complicates the code, creates confusion, opens a gap for bugs , better go for the easy, safe and obvious path.

public PessoaJuridica RetornarPessoaJuridica(string id) {
    return new PessoaJuridica();
}

public PessoaFisica RetornarPessoaFisica(string id) {
    return new PessoaFisica();
}

I placed GitHub for future reference .

    
14.08.2017 / 20:28
2

Two questions were posed in the post.

  

Can I create a return overload?

No , this really can not. The identity of a method is done by method name x parameters. Even if you duplicate the method by modifying only the return, the compiler will understand that it is the same method - not an overhead - and will give error.

  

Is it possible to offer return options in a method?

It is possible to do so, , just make this clear in the method signature.

A method may or may not return a value , but the legal thing is that value can be anything .

Using Either

public interface IPessoaRepository
{
    Either<Option<PessoaFisica>, Option<PessoaJuridica>> RetornaPessoa(string id);
}

public class PessoaRepository : IPessoaRepository
{
    public Either<Option<PessoaFisica>, Option<PessoaJuridica>> RetornaPessoa(string id)
    {
        // { seu codigo aqui }
        return pessoa; // Tanto faz se é tipo PessoaFisica ou PessoaJuridica
    }
}

Source: link

    
15.08.2017 / 12:12