locked
parse and tryparse RRS feed

  • Question

  • When to use Parse() and When to use tryparse() ?

    S.K Nayak

    Sunday, December 14, 2014 8:41 AM

Answers

  • Use Parse when you are sure that what you put in the method can be parsed to what you'd like.

    use TryParse: 1. When you are not sure that what you try to parse is in the correct format, so you want to avoid an error. 2. When you want to check if the input is in the correct format.

     

    Noam B.


    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    Sunday, December 14, 2014 12:05 PM
  • For example:

    string s = "1";

    string ss = "this is a string";

    and you want to convert s and ss to integer.

    for s, you should use parse

    int x = int.Parse(s);

    for ss, you should use TryParse to avoid an error.

    int y;

    bool b = int.TryParse(ss, out y); // if b = true you can convert ss to an integer, b = false => ss can not be converted to an integer.

    Sunday, December 14, 2014 2:30 PM

All replies

  • Use Parse when you are sure that what you put in the method can be parsed to what you'd like.

    use TryParse: 1. When you are not sure that what you try to parse is in the correct format, so you want to avoid an error. 2. When you want to check if the input is in the correct format.

     

    Noam B.


    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    Sunday, December 14, 2014 12:05 PM
  • For example:

    string s = "1";

    string ss = "this is a string";

    and you want to convert s and ss to integer.

    for s, you should use parse

    int x = int.Parse(s);

    for ss, you should use TryParse to avoid an error.

    int y;

    bool b = int.TryParse(ss, out y); // if b = true you can convert ss to an integer, b = false => ss can not be converted to an integer.

    Sunday, December 14, 2014 2:30 PM
  • Hi S.K Nayak,

    In a shorter way, If the string can not be converted to an integer, then

    • int.Parse() will throw an exception
    • int.TryParse() will return false (but not throw an exception)

    Good day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Cor Ligthert Wednesday, December 17, 2014 11:35 AM
    • Unproposed as answer by Cor Ligthert Wednesday, December 17, 2014 11:37 AM
    Wednesday, December 17, 2014 9:09 AM
  • Hi S.K Nayak,

    In a shorter way, If the string can not be converted to an integer, then

    • int.Parse() will throw an exception
    • int.TryParse() will return false (but not throw an exception)

    Good day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Kristin there is a typo in your text (integer can be Int64 but probably you mean DateTime structure)

    Success
    Cor

    Wednesday, December 17, 2014 11:37 AM