Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# params on method signature doesn't break an override / implementation

I have the following abstract base class:

public abstract class HashBase
{
    public abstract byte[] Hash(byte[] value);
}

And then I go ahead and implement that class:

public class CRC32Hash : HashBase
{
    public override byte[] Hash(params byte[] value)
    {
        return SomeRandomHashCalculator.Hash(value);
    }
}

Compile...and it works!

  1. Is this advised or does it lead to "evil" code?
  2. Is "params" sort of syntactic sugar?
like image 688
Matthew Layton Avatar asked May 16 '14 13:05

Matthew Layton


1 Answers

You can take a look to C# language specification §7.5.3 (overload)

Briefly, i think override keyword is used to redefine an implementation, not the parameters. You cant override args, the args must be the same of abstraction (im thinking about the liskov substitution principle application here).

Params is totally a syntaxic sugar, it is strictly equivalent to a simple array. It's just more easy to call in certain cases, avoiding array casting; compilator makes the work for you during the method call.

Note that in C# 6, params is going to be compatible with IEnumerable.

like image 191
nanderlini Avatar answered Oct 04 '22 20:10

nanderlini