Skip to content

Instantly share code, notes, and snippets.

View atsvetkov's full-sized avatar
🎧
tinkering

Alexander Tsvetkov atsvetkov

🎧
tinkering
View GitHub Profile
@atsvetkov
atsvetkov / dev_install
Last active January 25, 2016 21:37
boxstarter scripts
cinst wixtoolset -y
cinst firefox -y
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>
<ItemGroup>
public static class IntegerParser
{
private const int MinDigit = '0';
private const int MaxDigit = '9';
public static bool TryParse(object number, out int result)
{
if (number is int)
{
result = (int)number;
using System;
class Program
{
static void Main(string[] args)
{
foreach (var number in new object[] { 123, "2017", "-1", "1a2b3c", '5' })
{
try
{
public static bool TryParse(object number, out int result)
{
switch (number)
{
case int i:
result = i;
return true;
case string s:
return ParseString(s, out result);
default:
public static bool TryParse(object number, out int result)
{
switch (number)
{
case int i:
result = i;
return true;
case string s:
bool ParseString(string str, out int res)
{
try
{
if (IntegerParser.TryParse(number, out int result))
{
Console.WriteLine($"Parsed {number.GetType()} {number}, got {result}");
}
else
{
Console.WriteLine($"Could not parse {number.GetType()} {number}");
}
public class ParsingResult
{
public bool IsSuccessful { get; set; }
public int Result { get; set; }
}
public static ParsingResult TryParse(object number)
public static (bool success, int result) TryParse(object number)
{
switch (number)
{
case int i:
return (true, i);
case string s:
(bool isParsed, int value) ParseString(string str)
{
var res = 0;