0

I saw some other ways to take input from user in c# and it's really confusing. Can I use both the way to take input or is it only for float?

Is there more ways to take input?

float myAge;

myAge = Convert.Toint64(Console.ReadLine());

vs

float myAge;

float myAge = float.Parse(Console.ReadLine());
kkica
  • 4,034
  • 1
  • 20
  • 40

4 Answers4

3

I would recommend TryParse, to avoid exceptions in case user does not provide a valid float.

float myAge;
float.TryParse(Console.ReadLine(), out myAge);

or in 1 line float.TryParse(Console.ReadLine(), out float myAge);

TryParse will return a bool you can use to check if the value is a valid float.

if(float.TryParse(Console.ReadLine(), out myAge)){
   //do stuff
}else{
    Console.WriteLine("You did not give a float");
}

double, int have these methods too. Its not only for floats.

kkica
  • 4,034
  • 1
  • 20
  • 40
0
myAge = Convert.Toint64(Console.ReadLine())

Above will throw an exception when you enter a fraction as your input. Convert will attempt to converted to a 64bit integer and then get assigned to your variable myAge which is a float. If it is a fraction conversion will fail and throw exception.

float myAge = float.Parse(Console.ReadLine());

Above, with using float.Parse will retain the floating point number with fractions. However, will throw exception if it is non numeric.

Best option is to use TryPrase and confirm if entry is good:

float myAge;
while(!float.TryParse(Console.ReadLine(), out myAge))
{
   Console.WriteLine("Invalid entry, enter a number.");
}
loopedcode
  • 4,863
  • 1
  • 21
  • 21
0

You haven't stated what the desired data type is: float, Int64 (long), or something else? Seeing as how myAge is a float in both snippets that suggests you want the user to be able to input a float, so you should use a method that returns a float.

You could certainly read a long and store it in a float, but I don't see any reason to do that. Why let your variable store a fractional part if you won't let the user actually input one? If you read a long, store it in a long. If you store a float, call a method that reads a float.

Note that you are comparing Convert.ToInt64() to float.Parse(), when it seems like you should be comparing...

  • Convert.ToInt64() to Convert.ToSingle() (float being an alias for Single), and...
  • float.Parse() to long.Parse()

As others have mentioned, there are also TryParse() versions of the Parse() methods.

It all really comes down to personal preference. To me, parsing is the specific operation being performed, so I would choose a Parse() (or TryParse()) method. Some people might prefer the more general Convert.ToXXX() methods since they have overloads for many more datatypes beyond just string. In the end, Convert.ToInt64() is internally calling Int64.Parse(), anyways...

public static long ToInt64(string value) {
    if (value == null)
        return 0;
    return Int64.Parse(value, CultureInfo.CurrentCulture);
}

...and is documented as such...

Using the ToInt64(String) method is equivalent to passing value to the Int64.Parse(String) method.

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
0

I think the response exists here : Difference between int.Parse() and Convert.ToInt32

mlamsarf
  • 26
  • 6