Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In this program I did not understand by "Parse" ? What does the entire line "num1 = int.Parse(Console.ReadLine());" ?


* C# Program to Swap two Numbers
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
int num1, num2, temp;
Console.Write("\nEnter the First Number : ");
num1 = int.Parse(Console.ReadLine());
Console.Write("\nEnter the Second Number : ");
num2 = int.Parse(Console.ReadLine());
temp = num1;
num1 = num2;
num2 = temp;
Console.Write("\nAfter Swapping : ");
Console.Write("\nFirst Number : "+num1);
Console.Write("\nSecond Number : "+num2);
Console.Read();
}
}
}</pre>
Posted
Comments
Sergey Alexandrovich Kryukov 11-Jan-16 10:10am    
Re-post of http://www.codeproject.com/Questions/1070843/Why-do-we-use-Convert-ToInt-Console-ReadLine.

Please don't re-post. This is considered as abuse. Discuss everything on the page of your original question.

—SA

Really, you shouldn't.
Although Raje_ is right in what the code does, it makes no allowance for the inevitable user error. And when the user makes a mistake, the app crashes.
A better approach is to use TryParse:
C#
int num1;
while (true)
   {
   Console.Write("Enter the First Number : ");
   if (int.TryParse(Console.ReadLine(), out num1))
      {
      break;
      }
   Console.WriteLine("Please enter an integer value!");
   }

And I'd put that in a method that accepts a string and returns an int.
 
Share this answer
 
Comments
Member 12247039 11-Jan-16 6:50am    
Thanks. Though i am unable to understand your reply completely, i will go through it and get back if still there exists some doubt.
OriginalGriff 11-Jan-16 7:00am    
int.Parse tries to convert a string to an integer value. If it can;t do it because the user types "Hello", "123?", or "6.4" then it fails and throws an exception. If you don't explicitly catch the exception, then your program fails and the user is shown an error message. If you miskeyed when typing the nineteenth number in a set of twenty and the app crashed and you lost it all, you'd probably swear! (I certainly do).
int.TryParse tries to convert the string to an integer, but it doesn't fail if it can't - it returns a bool value which says "true" for "I converted it ok" or "false" for "I found a problem". That allows you to tell the user he made a mistake, and he can retype it without losing everything he typed before.
Member 12247039 11-Jan-16 7:02am    
Oh wow..thanks a ton for such a wonderful explanation. I get that right away...!!! Cheers..!!!
OriginalGriff 11-Jan-16 7:17am    
You're welcome!
Console.ReadLine reads input from the console. When the user presses enter, it returns a string. We then resume the program and process the string to determine the next action to take.

Source : http://www.dotnetperls.com/console-readline[^]

And
C#
int.Parse(Console.ReadLine());

It means you are converting user input value(which is a string) in to a integer.

For details go through above link.

Good luck.
 
Share this answer
 
Comments
Member 12247039 11-Jan-16 6:33am    
Is it then same as "Convert.ToInt32(Console.ReadLine());" ?
Member 12247039 11-Jan-16 6:33am    
Can both the statements be used interchangeably?
You should lookup the MSDN documentation for the specific functions. Then there should be no need to ask here (especially because you have already asked a similar question about Convert.ToInt32).

So enter the term using your preferred search engine:
"C# int.Parse"
and you will get among the first hits:
Int32.Parse Method (String) (System)[^]

For Convert.ToInt32:
Convert.ToInt32 Method (String, Int32) (System)[^]

Tip:
With the search results use those with the cryptic part at the end and not the links containing the function name. The first ones provide more information and example code.
 
Share this answer
 
Comments
Member 12247039 11-Jan-16 6:52am    
Thanks.
The answer is the same to your other question

Why do we use "Convert.ToInt32(Console.ReadLine());"?[^]

Convert.ToInt32 and int.Parse are basically the same thing.
 
Share this answer
 
Comments
Member 12247039 11-Jan-16 6:49am    
Thanks. I also asked the same question whether it is same to one of the earlier replies. So i can use either of them when need arises? Or is it one statement for a particular situation and the other statement for some other situation?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900