I have a method of type string that takes two arguments: string and double .

On output, I need to issue, as far as I understand, both arguments in string .

But an error occurs: the compiler cannot translate double to string . I tried different options: cast (string) before a double variable, double.ToString , ConvertToString , etc.

What method should I use?

The code itself:

 class Program { static void Main(string[] args) { Console.WriteLine(MyMethod("Hello", 11.0)); } private static string MyMethod(string word, double num) { num = Math.Round(num); string snum = num.ToString(); return MyMethod(word, num); } } 
  • 3
    What does на выходе мне нужно выдать насколько я понимаю оба аргумента в strin mean на выходе мне нужно выдать насколько я понимаю оба аргумента в strin ? Show your code. - sp7
  • 3
    The output can be only one argument (more precisely, the return value). Or an array. Show the code that you are trying to return. Maybe the problem is trying to return 2 values? - foxhound
  • 3
    The MyMethod method looks like endless recursion. What is the meaning of this method? - Regent
  • 2
    @Regent, I suspect he should have returned $"{word} {Math.Round(num)}" - Grundy
  • 2
    @Grundy yes, I also thought about it. But given the design of the question and the code, I would not be surprised if you really need to assemble a rocket and fly to the moon. - Regent

1 answer 1

 private static string MyMethod(string word, double num) { num = Math.Round(num); string snum = num.ToString(); return word + " " + snum; } 

Did you want this?
PS: Addition of lines - not the best option in terms of performance, but the most understandable beginner.

As for your question asked in comments: You have indicated in the method definition that it returns a string: private static string MyMethod ... That is, you want to get a string at the output of the method. return word + " " + snum; means: to return a string consisting of word, space and snum. What is required.

 return MyMethod(word, num); 

means completely different. This means something like the following: before exiting the method, once again call the MyMethod method with the word b num parameters, and return the result of working out this method. Therefore, in the last line you do not exit the method, but another instance of the method is called (this is called recursion). And so on to infinity. But in the case of a computer, infinity is limited by the amount of free memory. And as soon as you exceed this volume, the program crashes and informs you about this exception of StackOwerflow.

  • in this case, the addition does not affect performance - Grundy
  • Thank you very much, but why you cannot return in this form: return MyMethod (word, num); After all, this is more logical - Has
  • I 'll add to the answer in a couple of minutes - foxhound