Skip to content

Commit 3eb1afa

Browse files
Char array to string: net10.0, working solution file, new tests and a benchmark project (#2315)
The solution file mapped project GUIDs that did not exist, so CI built nothing and ran no tests. Regenerated it with all three projects. Both projects move to net10.0 with MSTest 4.5.1, Microsoft.NET.Test.Sdk 18.11.0 and coverlet.collector 10.1.0. Program.cs uses top-level statements and adds the part-of-array, separator and single-char examples. Five new tests cover them and the null-array behaviour. A BenchmarkDotNet 0.15.8 project compares the constructor, string.Concat and string.Join, and passes command-line args to the runner.
1 parent c6d13a6 commit 3eb1afa

8 files changed

Lines changed: 188 additions & 70 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using BenchmarkDotNet.Attributes;
2+
3+
namespace ConvertCharArray2String.Benchmarks;
4+
5+
[MemoryDiagnoser]
6+
public class CharArrayToStringBenchmarks
7+
{
8+
private char[] _charArray = [];
9+
10+
[Params(9, 1000)]
11+
public int Length { get; set; }
12+
13+
[GlobalSetup]
14+
public void Setup() =>
15+
_charArray = Enumerable.Range(0, Length).Select(i => (char)('a' + i % 26)).ToArray();
16+
17+
[Benchmark(Baseline = true)]
18+
public string StringConstructor() => new string(_charArray);
19+
20+
[Benchmark]
21+
public string StringConcat() => string.Concat(_charArray);
22+
23+
[Benchmark]
24+
public string StringJoin() => string.Join("", _charArray);
25+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using BenchmarkDotNet.Running;
2+
using ConvertCharArray2String.Benchmarks;
3+
4+
BenchmarkRunner.Run<CharArrayToStringBenchmarks>(args: args);
Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,62 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
4-
VisualStudioVersion = 17.1.31911.260
4+
VisualStudioVersion = 17.0.31903.59
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConvertCharArray2String", "ConvertCharArray2String\ConvertCharArray2String.csproj", "{F21AE7B0-5961-431F-9573-CF567C362730}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConvertCharArray2String", "ConvertCharArray2String\ConvertCharArray2String.csproj", "{CC387591-8080-46EE-8045-62FB77775F15}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConvertCharArray2StringTests", "ConvertCharArray2StringTests\ConvertCharArray2StringTests.csproj", "{8359457A-7DEA-438D-9A3B-8725C41729C8}"
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConvertCharArray2StringTests", "ConvertCharArray2StringTests\ConvertCharArray2StringTests.csproj", "{269D6433-F548-49D7-AE0A-346ADBDEAFF3}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConvertCharArray2String.Benchmarks", "ConvertCharArray2String.Benchmarks\ConvertCharArray2String.Benchmarks.csproj", "{9FF511B2-F0CB-4DD1-9EFF-965E49B7D636}"
911
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
15+
Debug|x64 = Debug|x64
16+
Debug|x86 = Debug|x86
1317
Release|Any CPU = Release|Any CPU
18+
Release|x64 = Release|x64
19+
Release|x86 = Release|x86
1420
EndGlobalSection
1521
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16-
{E526D224-B8CE-4E46-AF6C-AB5A3987323C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17-
{E526D224-B8CE-4E46-AF6C-AB5A3987323C}.Debug|Any CPU.Build.0 = Debug|Any CPU
18-
{E526D224-B8CE-4E46-AF6C-AB5A3987323C}.Release|Any CPU.ActiveCfg = Release|Any CPU
19-
{E526D224-B8CE-4E46-AF6C-AB5A3987323C}.Release|Any CPU.Build.0 = Release|Any CPU
20-
{C1B5D5EE-1714-49E8-BAAA-5A31DA4C852E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21-
{C1B5D5EE-1714-49E8-BAAA-5A31DA4C852E}.Debug|Any CPU.Build.0 = Debug|Any CPU
22-
{C1B5D5EE-1714-49E8-BAAA-5A31DA4C852E}.Release|Any CPU.ActiveCfg = Release|Any CPU
23-
{C1B5D5EE-1714-49E8-BAAA-5A31DA4C852E}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{CC387591-8080-46EE-8045-62FB77775F15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{CC387591-8080-46EE-8045-62FB77775F15}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{CC387591-8080-46EE-8045-62FB77775F15}.Debug|x64.ActiveCfg = Debug|Any CPU
25+
{CC387591-8080-46EE-8045-62FB77775F15}.Debug|x64.Build.0 = Debug|Any CPU
26+
{CC387591-8080-46EE-8045-62FB77775F15}.Debug|x86.ActiveCfg = Debug|Any CPU
27+
{CC387591-8080-46EE-8045-62FB77775F15}.Debug|x86.Build.0 = Debug|Any CPU
28+
{CC387591-8080-46EE-8045-62FB77775F15}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{CC387591-8080-46EE-8045-62FB77775F15}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{CC387591-8080-46EE-8045-62FB77775F15}.Release|x64.ActiveCfg = Release|Any CPU
31+
{CC387591-8080-46EE-8045-62FB77775F15}.Release|x64.Build.0 = Release|Any CPU
32+
{CC387591-8080-46EE-8045-62FB77775F15}.Release|x86.ActiveCfg = Release|Any CPU
33+
{CC387591-8080-46EE-8045-62FB77775F15}.Release|x86.Build.0 = Release|Any CPU
34+
{269D6433-F548-49D7-AE0A-346ADBDEAFF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{269D6433-F548-49D7-AE0A-346ADBDEAFF3}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{269D6433-F548-49D7-AE0A-346ADBDEAFF3}.Debug|x64.ActiveCfg = Debug|Any CPU
37+
{269D6433-F548-49D7-AE0A-346ADBDEAFF3}.Debug|x64.Build.0 = Debug|Any CPU
38+
{269D6433-F548-49D7-AE0A-346ADBDEAFF3}.Debug|x86.ActiveCfg = Debug|Any CPU
39+
{269D6433-F548-49D7-AE0A-346ADBDEAFF3}.Debug|x86.Build.0 = Debug|Any CPU
40+
{269D6433-F548-49D7-AE0A-346ADBDEAFF3}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{269D6433-F548-49D7-AE0A-346ADBDEAFF3}.Release|Any CPU.Build.0 = Release|Any CPU
42+
{269D6433-F548-49D7-AE0A-346ADBDEAFF3}.Release|x64.ActiveCfg = Release|Any CPU
43+
{269D6433-F548-49D7-AE0A-346ADBDEAFF3}.Release|x64.Build.0 = Release|Any CPU
44+
{269D6433-F548-49D7-AE0A-346ADBDEAFF3}.Release|x86.ActiveCfg = Release|Any CPU
45+
{269D6433-F548-49D7-AE0A-346ADBDEAFF3}.Release|x86.Build.0 = Release|Any CPU
46+
{9FF511B2-F0CB-4DD1-9EFF-965E49B7D636}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
47+
{9FF511B2-F0CB-4DD1-9EFF-965E49B7D636}.Debug|Any CPU.Build.0 = Debug|Any CPU
48+
{9FF511B2-F0CB-4DD1-9EFF-965E49B7D636}.Debug|x64.ActiveCfg = Debug|Any CPU
49+
{9FF511B2-F0CB-4DD1-9EFF-965E49B7D636}.Debug|x64.Build.0 = Debug|Any CPU
50+
{9FF511B2-F0CB-4DD1-9EFF-965E49B7D636}.Debug|x86.ActiveCfg = Debug|Any CPU
51+
{9FF511B2-F0CB-4DD1-9EFF-965E49B7D636}.Debug|x86.Build.0 = Debug|Any CPU
52+
{9FF511B2-F0CB-4DD1-9EFF-965E49B7D636}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{9FF511B2-F0CB-4DD1-9EFF-965E49B7D636}.Release|Any CPU.Build.0 = Release|Any CPU
54+
{9FF511B2-F0CB-4DD1-9EFF-965E49B7D636}.Release|x64.ActiveCfg = Release|Any CPU
55+
{9FF511B2-F0CB-4DD1-9EFF-965E49B7D636}.Release|x64.Build.0 = Release|Any CPU
56+
{9FF511B2-F0CB-4DD1-9EFF-965E49B7D636}.Release|x86.ActiveCfg = Release|Any CPU
57+
{9FF511B2-F0CB-4DD1-9EFF-965E49B7D636}.Release|x86.Build.0 = Release|Any CPU
2458
EndGlobalSection
2559
GlobalSection(SolutionProperties) = preSolution
2660
HideSolutionNode = FALSE
2761
EndGlobalSection
28-
GlobalSection(ExtensibilityGlobals) = postSolution
29-
SolutionGuid = {6A5466CD-321D-46D4-B2B3-1E36EAF17EEC}
30-
EndGlobalSection
3162
EndGlobal

‎strings-csharp/ConvertCharArray2String/ConvertCharArray2String/ConvertCharArray2String.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1-
using System.Text;
2-
3-
namespace ConvertCharArray2String
4-
{
5-
public class Program
6-
{
7-
public static void Main(string[] args)
8-
{
9-
char[] charArray = { 'c', 'o', 'd', 'e', ' ', 'm', 'a', 'z', 'e' };
10-
11-
var convertedToString = new string(charArray);
12-
Console.WriteLine(convertedToString);
13-
14-
var joinedToString = string.Join("", charArray);
15-
Console.WriteLine(joinedToString);
16-
17-
var concatedToString = string.Concat(charArray);
18-
Console.WriteLine(concatedToString);
19-
20-
Console.ReadLine();
21-
}
22-
}
23-
}
1+
char[] charArray = ['c', 'o', 'd', 'e', ' ', 'm', 'a', 'z', 'e'];
2+
3+
var convertedToString = new string(charArray);
4+
5+
Console.WriteLine(convertedToString);
6+
7+
var partOfArray = new string(charArray, 0, 4);
8+
9+
Console.WriteLine(partOfArray);
10+
11+
var joinedToString = string.Join("", charArray);
12+
13+
Console.WriteLine(joinedToString);
14+
15+
var withDashes = string.Join("-", charArray);
16+
17+
Console.WriteLine(withDashes);
18+
19+
var concatedToString = string.Concat(charArray);
20+
21+
Console.WriteLine(concatedToString);
22+
23+
char letter = 'c';
24+
25+
var fromChar = letter.ToString();
26+
27+
Console.WriteLine(fromChar);
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
56
<Nullable>enable</Nullable>
6-
77
<IsPackable>false</IsPackable>
88
</PropertyGroup>
99

1010
<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" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="4.5.1" />
13+
<PackageReference Include="MSTest.TestFramework" Version="4.5.1" />
14+
<PackageReference Include="coverlet.collector" Version="10.1.0" />
1515
</ItemGroup>
1616

1717
</Project>
Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,78 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22

3-
namespace ConvertCharArray2StringTests
3+
namespace ConvertCharArray2StringTests;
4+
5+
[TestClass]
6+
public class ConvertCharArrayTests
47
{
5-
[TestClass]
6-
public class ConvertCharArrayTests
8+
private readonly char[] _charArray = ['c', 'o', 'd', 'e', ' ', 'm', 'a', 'z', 'e'];
9+
10+
[TestMethod]
11+
public void GivenCharArray_WhenConvertedByConstructor_ThenReturnString()
12+
{
13+
var convertedToString = new string(_charArray);
14+
15+
Assert.AreEqual("code maze", convertedToString);
16+
}
17+
18+
[TestMethod]
19+
public void GivenCharArray_WhenConvertedByJoin_ThenReturnString()
20+
{
21+
var joinedToString = string.Join("", _charArray);
22+
23+
Assert.AreEqual("code maze", joinedToString);
24+
}
25+
26+
[TestMethod]
27+
public void GivenCharArray_WhenConvertedByConcat_ThenReturnString()
28+
{
29+
var concatedToString = string.Concat(_charArray);
30+
31+
Assert.AreEqual("code maze", concatedToString);
32+
}
33+
34+
[TestMethod]
35+
public void GivenCharArray_WhenConvertedByConstructorWithRange_ThenReturnPartOfString()
736
{
8-
[TestMethod]
9-
public void GivenCharArray_WhenConvertedByConstructor_ThenReturnString()
10-
{
11-
char[] charArray = { 'c', 'o', 'd', 'e', ' ', 'm', 'a', 'z', 'e' };
37+
var partOfArray = new string(_charArray, 0, 4);
1238

13-
var convertedToString = new string(charArray);
14-
15-
Assert.AreEqual("code maze", convertedToString);
16-
}
39+
Assert.AreEqual("code", partOfArray);
40+
}
1741

18-
[TestMethod]
19-
public void GivenCharArray_WhenConvertedByJoin_ThenReturnString()
20-
{
21-
char[] charArray = { 'c', 'o', 'd', 'e', ' ', 'm', 'a', 'z', 'e' };
42+
[TestMethod]
43+
public void GivenSingleChar_WhenConvertedByToString_ThenReturnString()
44+
{
45+
char letter = 'c';
2246

23-
var joinedToString = string.Join("", charArray);
47+
var fromChar = letter.ToString();
2448

25-
Assert.AreEqual("code maze", joinedToString);
26-
}
49+
Assert.AreEqual("c", fromChar);
50+
}
2751

28-
[TestMethod]
29-
public void GivenCharArray_WhenConvertedByConcat_ThenReturnString()
30-
{
31-
char[] charArray = { 'c', 'o', 'd', 'e', ' ', 'm', 'a', 'z', 'e' };
52+
[TestMethod]
53+
public void GivenCharArray_WhenJoinedWithSeparator_ThenReturnSeparatedString()
54+
{
55+
var withDashes = string.Join("-", _charArray);
56+
57+
Assert.AreEqual("c-o-d-e- -m-a-z-e", withDashes);
58+
}
3259

33-
var concatedToString = string.Concat(charArray);
60+
[TestMethod]
61+
public void GivenNullCharArray_WhenConvertedByConstructor_ThenReturnEmptyString()
62+
{
63+
char[]? nothing = null;
64+
65+
var convertedToString = new string(nothing);
66+
67+
Assert.AreEqual(string.Empty, convertedToString);
68+
}
69+
70+
[TestMethod]
71+
public void GivenNullCharArray_WhenConvertedByConcatOrJoin_ThenThrowArgumentNullException()
72+
{
73+
char[]? nothing = null;
3474

35-
Assert.AreEqual("code maze", concatedToString);
36-
}
75+
Assert.ThrowsExactly<ArgumentNullException>(() => string.Concat(nothing!));
76+
Assert.ThrowsExactly<ArgumentNullException>(() => string.Join("", nothing!));
3777
}
38-
}
78+
}

0 commit comments

Comments
 (0)