Fix CA1825: Avoid zero-length array allocations
Initializing a zero-length array leads to an unnecessary memory allocation. Instead, use the statically allocated empty array instance by calling the Array.Empty method. The memory allocation is shared across all invocations of this method.
MODIFY
src/Chapter03.Tests/Listing03.01.Tests.cs
src/Chapter03.Tests/Listing03.01.Tests.cs
@@ -19,7 +19,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Listing03_01.Tests
[TestMethod]
public void Main_GivenValidString_MakeUppercase()
{
- string[] args = { };
+ string[] args = System.Array.Empty<string>();
const string expected =
"\'number\' requires a value and cannot be null";

MODIFY
src/Chapter05.Tests/Listing05.12.PassingCommandLineArgumentsToMain.Tests.cs
src/Chapter05.Tests/Listing05.12.PassingCommandLineArgumentsToMain.Tests.cs
@@ -10,7 +10,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_12.Tests
[TestMethod]
public void Main_NoArgs_ExpectErrors()
{
- string[] args = new string[0];
+ string[] args = Array.Empty<string>();
string view =
@"ERROR: You must specify the URL and the file name
Usage: Downloader.exe <URL> <TargetFileName>";

MODIFY
src/Chapter05.Tests/Listing05.15.PassingVariablesOutOnly.Tests.cs
src/Chapter05.Tests/Listing05.15.PassingVariablesOutOnly.Tests.cs
@@ -8,7 +8,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_15.Tests
[TestMethod]
public void Main_InputNoPhrase_ExpectHelp()
{
- string[] args = new string[0];
+ string[] args = System.Array.Empty<string>();
string view =
@"ConvertToPhoneNumber.exe <phrase>
'_' indicates no standard phone button";

MODIFY
src/Chapter06.Tests/Listing06.37.UsingTheMaybeNullAttribute.Tests.cs
src/Chapter06.Tests/Listing06.37.UsingTheMaybeNullAttribute.Tests.cs
@@ -16,7 +16,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_37.Tests
public void GetObject_GivenEmptyCollection_ReturnNull()
{
Assert.IsNull(NullabilityAttributesExamined.GetObject(
- new string[] { }, item => item is object));
+ System.Array.Empty<string>(), item => item is object));
}
[TestMethod]

MODIFY
src/Chapter06.Tests/Listing06.45.DeclaringAStaticClass.Tests.cs
src/Chapter06.Tests/Listing06.45.DeclaringAStaticClass.Tests.cs
@@ -10,7 +10,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_45.Tests
public void MainTest_ArgsHasZeroElements_ThrowException()
{
Assert.ThrowsException<ArgumentException>(
- () => Program.Main(new string[] { }));
+ () => Program.Main(Array.Empty<string>()));
}
[TestMethod]

MODIFY
src/Chapter06/Listing06.37.PotentialNullReturnWithMaybeNullAttribute.cs
src/Chapter06/Listing06.37.PotentialNullReturnWithMaybeNullAttribute.cs
@@ -8,7 +8,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_37
public class NullabilityAttributesExamined
{
static public string? Method() =>
- GetObject(new string[0], (item) => true);
+ GetObject(Array.Empty<string>(), (item) => true);
[return: MaybeNull]
static public T GetObject<T>(

MODIFY
src/Chapter08.Tests/Listing08.02.ImplementingAndUsingInterfaces.Tests.cs
src/Chapter08.Tests/Listing08.02.ImplementingAndUsingInterfaces.Tests.cs
@@ -48,7 +48,7 @@ The Hitchhiker's Guide to the Galaxy Douglas Adams 1979
[TestMethod]
public void List_NoItemsInArray_HeadersProperlyDisplayed()
{
- IListable[] contacts = {};
+ IListable[] contacts = Array.Empty<IListable>();
string expected = "Header1Header2";

MODIFY
src/Chapter08.Tests/Listing08.10.InterfaceExtensionMethods.Tests.cs
src/Chapter08.Tests/Listing08.10.InterfaceExtensionMethods.Tests.cs
@@ -43,7 +43,7 @@ The Hitchhiker's Guide to the Galaxy Douglas Adams 1979
[TestMethod]
public void List_NoItemsInArray_HeadersProperlyDisplayed()
{
- IListable[] contacts = {};
+ IListable[] contacts = Array.Empty<IListable>();
string expected = "Header1Header2";

MODIFY
src/Chapter18.Tests/Listing18.03.Tests.cs
src/Chapter18.Tests/Listing18.03.Tests.cs
@@ -13,7 +13,7 @@ Compress.exe /Out:< file name > /Help /Priority:RealTime | High | AboveNormal |
IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected,
() =>
{
- Program.Main(new string[]{ });
+ Program.Main(System.Array.Empty<string>());
});
}

Fix CA1825: Avoid zero-length array allocations

Initializing a zero-length array leads to an unnecessary memory allocation. Instead, use the statically allocated empty array instance by calling the Array.Empty method. The memory allocation is shared across all invocations of this method.