Here is a demonstration of the problem:
class Program { static double Func(double a, double b) { return a * 1000 + b * b; } static void Main(string[] args) { var a = 1.1d; var b = 2.2d; Console.WriteLine(Func(a, b));
This becomes a problem if there are methods with many parameters (for example, ten out of double ):
double Func(double parameter1, double parameter2, ..., double parameter10);
Question: is there a way to check the parameters when calling the method so that the programmer is less prone to errors?
This is not a problem if the parameter types are different. I thought it might be due to new types:
class A { private double _value; public static implicit operator A(double value) { return new A() { _value = value }; } public static implicit operator double(A value) { return value._value; } } class B { private double _value; public static implicit operator B(double value) { return new B() { _value = value }; } public static implicit operator double(B value) { return value._value; } } class Program { static double Func(A a, B b) { return a * 1000 + b * b; } static void Main(string[] args) { A a = 1.1d; B b = 2.2d; Console.WriteLine(Func(a, b)); Console.WriteLine(Func(b, a));
And this is so, but I'm completely not sure if this method is effective. I have doubts if this is a good idea. It? Or is it better there?
I know that I can be absolutely safe if I always call a method like this (using the named arguments method)
Func(a:a, b:b);
This should not lead to overhead in the code, but a lot of typing. A wrapper is better because it runs once (creating a new type is easy), but it probably has overhead.
source share