2- Convert a String to long without using C#’s built in function?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringToInteger
{
class Program
{

private static int StrToInt(string str)
{
int intResult = 0;
int index = 0;
bool negative = false;
int len = str.Length;

if (str[0] == '-')
{
negative = true;
index = 1;
}

while (index < len)
{
intResult *= 10;
intResult += (str[index++] - '0');
}

if (negative)
intResult *= -1;

return intResult;
}

static void Main(string[] args)
{
string str1 = "-102990";

Console.WriteLine("{0} = {1}", str1, StrToInt(str1).ToString());

Console.ReadLine();
}
}
}

Advertisements