Skip to content

Instantly share code, notes, and snippets.

@luisdeol
Created March 29, 2020 13:56
Show Gist options
  • Save luisdeol/020c681fac7bc0a2eb5c8312b05fe8af to your computer and use it in GitHub Desktop.
Save luisdeol/020c681fac7bc0a2eb5c8312b05fe8af to your computer and use it in GitHub Desktop.
3.1: Use built-in functions to validate data type and content
using System;
namespace _31_ValidateAppInput
{
class Program
{
static void Main(string[] args)
{
const string decimalString = "10.4";
const string intString = "50";
const string invalidIntString = "FIVE";
// Valid conversions
var decimalValid = decimal.Parse(decimalString);
Console.WriteLine($"Conversion succesfully performed with decimal.Parse for {decimalValid}.");
if (int.TryParse(intString, out int intSTryParse))
{
Console.WriteLine("Conversion succesfully performed with int.TryParse.");
} else
{
Console.WriteLine("Conversion failed when performed using int.TryParse.");
}
var intConvert = Convert.ToInt32(intString);
Console.WriteLine($"Conversion succesfully performed with int.Parse for {intConvert}.");
// Invalid conversions
try
{
var invalidInt = int.Parse(invalidIntString);
} catch(FormatException)
{
Console.WriteLine($"int.Parse failed for {invalidIntString}");
}
if (!int.TryParse(invalidIntString, out int intTryParse))
{
Console.WriteLine($"int.TryParse returned false for {invalidIntString}");
}
try
{
var intConvertInvalid = Convert.ToInt32(invalidIntString);
} catch(FormatException)
{
Console.WriteLine($"Convert.ToInt32 failed for {invalidIntString}");
}
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment