6

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?
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • 6
    *“Is "params" sort of syntactic sugar?”* Yep! If you pass an array, it will use that array; if you pass some things that could be elements of an array, it will make those into an array. So it’s compatible; I don’t know about whether it’s advised. – Ry- May 16 '14 at 13:57

1 Answers1

1

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.

nanderlini
  • 36
  • 4