casting - How to cast double or double array into double array always in C# -
casting - How to cast double or double array into double array always in C# -
i have method process double arrays. supplying double arrays using keyword params
. problem sometimesi have pass doubles , want utilize same method both types. method have signature
public void dosomething(params double[][] arrays)
above method definition works fine next arguments:
double [] array = new double []{2,3,5} ; double [] anotherarray = new double []{7,8,10} ; dosomething (array, anotherarray) ;
i thing pass object , cast them in method , utilize seek grab block , not know right approach or there exists elegant way handle kind of situation because can have mixed info input arguments.
public void dosomething(params object objs) { // loop seek { var tempdata = (double) objs(loop index); double[] info = new double[] { tempdata }; } grab { var tempdata = (double []) objs(loop index); double [] info = tempdata; } // other opertaion }
i want phone call way:
double [] array = new double []{2,3,5} ; double singlevalue = 10 ; double [] anotherarray = new double []{7,8,10} ; dosomething (array, singlevalue, anotherarray) ;
now you've given illustration of how want phone call it, suspect using dynamic typing may simplest approach:
public void dosomething(params object[] values) { foreach (dynamic value in values) { // overload resolution performed @ execution time dosomethingimpl(value); } } private void dosomethingimpl(double value) { ... } private void dosomethingimpl(double[] value) { ... }
you manually if want:
public void dosomething(params object[] values) { foreach (object value in values) { if (value double) { dosomethingimpl((double) value); // or: dosomethingimpl(new[] { (double) value }); } else if (value double[]) { dosomethingimpl((double[]) value); } ... } }
i not cast unconditionally , grab exception that's thrown. that's horrible abuse of exceptions.
c# casting
Comments
Post a Comment