c# - Can an interface have a templated method name? -
i know answer no, still confirm.
can interface have variable method name?
interface ivariablemethodname //something similar <t> { void t(int param); void t(string param); }
i have classes require function overload couple functions same parameter different name , have different functionality. there better way instead of making bunch interfaces same content except method name?
edited: example
class adder { public int add(int a, int b){} public float add(float a, float b){} public decimal add(decimal a, decimal b){} } class subber { public int sub(int a, int b){} public float sub(float a, float b){} public decimal sub(decimal a, decimal b){} }
what trying similar examples difference return type class itself. , methods have different functionality.
i still not sure understand question completely. however, sure not possible define single interface interface members can take on different names depending on implementing class.
given underlying goal 1 of maintainance — is, ensure compile-time error if class fails implement of necessary methods — seems me closest 1 can come declare interface general-purpose method names, , have each specific type implement interface through delegation type-specific methods.
for example:
interface ioperator { int operation(int a, int b); float operation(float a, float b); decimal operation(decimal a, decimal b); } class adder : ioperator { public int add(int a, int b){} public float add(float a, float b){} public decimal add(decimal a, decimal b){} public int operation(int a, int b) { return add(a, b); } public float operation(float a, float b) { return add(a, b); } public decimal operation(decimal a, decimal b) { return add(a, b); } } class subber : ioperator { public int sub(int a, int b){} public float sub(float a, float b){} public decimal sub(decimal a, decimal b){} public int operation(int a, int b) { return sub(a, b); } public float operation(float a, float b) { return sub(a, b); } public decimal operation(decimal a, decimal b) { return sub(a, b); } }
unfortunately, not guarantee each type has implemented type-specific methods in question. require each type implements interface, , if have developers on team implement new method in interface means other adding corresponding type-specific method class , calling it, have bigger problems @ hand. :)
it seems unfortunate me interface might not used in way interfaces are: i.e. support polymorphic implementation of specific contract. maybe you'd find once have in place, makes sense go ahead , generalize other code.
for matter, if point, maybe you'll find makes more sense stop writing these types different method names in each type, given across different types methods same. :)
Comments
Post a Comment