C # parameter on method signature does not violate override / implementation

I have the following abstract base class:

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

And then I go and implement this class:

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

Compile ... and it works!

  • Is this advice or does it lead to "evil" code?
  • Is syntactic sugar "parameters"?
+6
source share
1 answer

You can take a look at the C # language specification ยง7.5.3 (overload)

In short, I think the override keyword is used to override implementations , not parameters. You cannot redefine args, args must be the same from abstraction (they think about applying the liskov signature principle here).

Params is fully syntactic sugar, it is strictly equivalent to a simple array. In some cases, this is easier to find, avoiding array casts; the compiler does the work for you during the method call.

Note that in C # 6 the options will be compatible with IEnumerable.

+1
source

Source: https://habr.com/ru/post/969378/


All Articles