Skip to content

Commit 9881c5e

Browse files
authored
CM-235: Difference between int.Parse() and Convert.ToInt32 in C# (#197)
1 parent 457a13a commit 9881c5e

File tree

5 files changed

+203
-0
lines changed

5 files changed

+203
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31919.166
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntParseVsConvertToInt", "IntParseVsConvertToInt\IntParseVsConvertToInt.csproj", "{2DA8495D-1230-4BA9-9E68-B16B6A9F3743}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{BF213CC4-67F1-46BF-AC07-5FE38FA9A234}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{2DA8495D-1230-4BA9-9E68-B16B6A9F3743}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{2DA8495D-1230-4BA9-9E68-B16B6A9F3743}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{2DA8495D-1230-4BA9-9E68-B16B6A9F3743}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{2DA8495D-1230-4BA9-9E68-B16B6A9F3743}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{BF213CC4-67F1-46BF-AC07-5FE38FA9A234}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{BF213CC4-67F1-46BF-AC07-5FE38FA9A234}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{BF213CC4-67F1-46BF-AC07-5FE38FA9A234}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{BF213CC4-67F1-46BF-AC07-5FE38FA9A234}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {7066FD66-2D00-4284-8731-2EDDC3A70AC7}
30+
EndGlobalSection
31+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+

2+
namespace IntParseVsConvertToInt
3+
{
4+
public class Program
5+
{
6+
public static void IntParseMethod(string inputString)
7+
{
8+
try
9+
{
10+
var outputInteger = int.Parse(inputString);
11+
Console.WriteLine(outputInteger);
12+
}
13+
catch (ArgumentNullException ex)
14+
{
15+
Console.WriteLine("The int.Parse() method throws ArgumentNullException.");
16+
}
17+
}
18+
19+
public static void ConvertToInt32Method(string inputString)
20+
{
21+
var outputInteger = Convert.ToInt32(inputString);
22+
Console.WriteLine(outputInteger);
23+
}
24+
25+
public static void IntTryParseMethod(string inputString)
26+
{
27+
if (int.TryParse(inputString, out var outputInteger))
28+
Console.WriteLine($"Output value: {outputInteger}");
29+
else
30+
Console.WriteLine("The conversion failed.");
31+
}
32+
33+
public static void Main(string[] args)
34+
{
35+
var inputString = " 123 ";
36+
string inputNullString = null;
37+
38+
IntParseMethod(inputString);
39+
IntParseMethod(inputNullString);
40+
41+
Console.WriteLine();
42+
43+
ConvertToInt32Method(inputString);
44+
ConvertToInt32Method(inputNullString);
45+
46+
Console.WriteLine();
47+
48+
IntTryParseMethod(inputString);
49+
IntTryParseMethod(inputNullString);
50+
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
4+
namespace Tests
5+
{
6+
[TestClass]
7+
public class IntParseVsConvertToIntUnitTest
8+
{
9+
[TestMethod]
10+
public void WhenPassCorrectFromatedStringToIntParse_ThenCorrectOutputInteger()
11+
{
12+
var input = "345 ";
13+
var expected = 345;
14+
var actual = 0;
15+
try
16+
{
17+
actual = int.Parse(input);
18+
19+
}
20+
catch (ArgumentNullException ex)
21+
{
22+
Console.WriteLine("ArgumentNullException");
23+
}
24+
25+
Assert.AreEqual(expected, actual);
26+
}
27+
28+
[TestMethod]
29+
public void WhenPassNullStringToIntParse_ThenNullException()
30+
{
31+
string input = null;
32+
var expected = "ArgumentNullException";
33+
var output = 0;
34+
var actual = string.Empty;
35+
try
36+
{
37+
output = int.Parse(input);
38+
39+
}
40+
catch (ArgumentNullException ex)
41+
{
42+
actual = "ArgumentNullException";
43+
}
44+
45+
Assert.AreEqual(expected, actual);
46+
}
47+
48+
[TestMethod]
49+
public void WhenPassCorrectFromatedStringToConvertToInt_ThenCorrectOutputInteger()
50+
{
51+
var input = "345 ";
52+
var expected = 345;
53+
var actual = Convert.ToInt32(input);
54+
55+
Assert.AreEqual(expected, actual);
56+
}
57+
58+
[TestMethod]
59+
public void WhenPassNullStringToConvertToInt_ThenZero()
60+
{
61+
string input = null;
62+
var expected = 0;
63+
var actual = Convert.ToInt32(input);
64+
65+
Assert.AreEqual(expected, actual);
66+
}
67+
68+
[TestMethod]
69+
public void WhenPassCorrectFromatedStringToIntTryParse_ThenCorrectOutputIntegerWIthReturnTrue()
70+
{
71+
var input = "345 ";
72+
var expected = 345;
73+
var expectedSuccess = true;
74+
var actualSuccess = int.TryParse(input, out var actual);
75+
76+
Assert.AreEqual(expected, actual);
77+
Assert.AreEqual(expectedSuccess, actualSuccess);
78+
}
79+
80+
[TestMethod]
81+
public void WhenPassNullStringToIntTryParse_ThenReturnFalse()
82+
{
83+
string input = null;
84+
var expected = 0;
85+
var expectedSuccess = false;
86+
var actualSuccess = int.TryParse(input, out var actual);
87+
88+
Assert.AreEqual(expected, actual);
89+
Assert.AreEqual(expectedSuccess, actualSuccess);
90+
}
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
13+
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
14+
<PackageReference Include="coverlet.collector" Version="3.1.0" />
15+
</ItemGroup>
16+
17+
</Project>

0 commit comments

Comments
 (0)