+ 2

(Solved)if a variable is already declare as double why should i convert it?

Hello everyone! I just have a question about 1st code project . Why should I convert radius to double if it is already declare as double? does it mean I have to do it for the reason of user input in Console.ReadLine(); ? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test { internal class Program { static void Main(string[] args) { const double pi = 3.14; double radius; radius = Convert.ToDouble(Console.ReadLine()); double r = radius; double area = pi * r * r; Console.WriteLine(area); } } } edit: Thanks everyone for your help =)

15th Dec 2021, 12:13 PM
Valerio
Valerio - avatar
4 Respostas
+ 5
Because ReadLine() will return only strings
15th Dec 2021, 12:14 PM
Lisa
Lisa - avatar
+ 4
Valeriy Most of the language returns string when you take input. C# Python JavaScript Ruby Swift Kotlin - in case of readLine() Returns string bydefault
15th Dec 2021, 2:37 PM
A͢J
A͢J - avatar
+ 3
"Because ReadLine() will return only strings" Alright it makes sense. Thank you.
15th Dec 2021, 12:18 PM
Valerio
Valerio - avatar
+ 1
ReadLine() is a method available in the Console class By default, it will take everything as a string The following reasons behind this ====================== In c language we c a problem with real numbers By default, it will consider every as a double value which is memory wastage This is happening due to auto-convertion The same thing happening in CPP To overcome this from java onwards we are using wrapper classes In java: Double.parseDouble(); In c# : Convert.toDouble64(); or double.Parse(Console.ReadLine()); In the above case, it is up to developers whether they need float or double value Now there is no chance of memory wastage or lack of memory for variables
17th Dec 2021, 7:07 AM
sree harsha
sree harsha - avatar