Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Span<T> example #556

Merged
merged 10 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion exclusion.dic
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ Parksdale
Monteray
inigo
Machava
Uncompress
Uncompress
expialidocious

This file was deleted.

17 changes: 17 additions & 0 deletions src/Chapter17.Tests/Listing17.09.SlicingWithSpan.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_09.Tests;

[TestClass]
public class ProgramTests
{
[TestMethod]
public void DictionaryInitialization()
{
IntelliTect.TestTools.Console.ConsoleAssert.Execute(null,
() =>
{
Program.Main();
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_11.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_12.Tests;

[TestClass]
public class ProgramTests
Expand All @@ -15,4 +15,4 @@ public void IteratingABinaryTree()
IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected,
Program.Main);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_13.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_14.Tests;

[TestClass]
public class ProgramTests
Expand Down Expand Up @@ -28,4 +28,4 @@ public void MainTest()
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, Program.Main);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_15.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_16.Tests;

[TestClass]
public class ProgramTests
Expand All @@ -14,4 +14,4 @@ public void MainTest()
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, Program.Main);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_17.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_18.Tests;

[TestClass]
public class ProgramTests
Expand All @@ -20,4 +20,4 @@ John Francis Fitzgerald
IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected,
() => Program.Main());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_20.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_21.Tests;

[TestClass]
public class ProgramTests
Expand All @@ -14,4 +14,4 @@ public void MainTest()
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, Program.Main);
}
}
}
7 changes: 5 additions & 2 deletions src/Chapter17/Chapter17.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
</PropertyGroup>
<Import Project="..\Chapter.props" />
<ItemGroup>
<Compile Remove="Listing17.19.CSharpEquivalentOfCompilerGeneratedCodeForIterators.cs" />
<Compile Remove="Listing17.20.CSharpEquivalentOfCompilerGeneratedCodeForIterators.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Listing17.19.CSharpEquivalentOfCompilerGeneratedCodeForIterators.cs" />
<None Remove="Listing17.07a.IteratingOverSortedDictionaryWithForeach.Placeholder.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Listing17.20.CSharpEquivalentOfCompilerGeneratedCodeForIterators.cs" />
<Compile Include="..\Shared\Program.cs">
<Link>Program.cs</Link>
</Compile>
Expand Down
51 changes: 51 additions & 0 deletions src/Chapter17/Listing17.09.SlicingWithSpan.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_09;

using System.Diagnostics;
using System.Runtime.CompilerServices;

public class Program
{
public static void Main()
{
#region INCLUDE
string[] languages = new [] {
"C#", "COBOL", "Java",
"C++", "TypeScript", "Python",};

// Create a Span<string> from the arrays first 3 elements.
Span<string> languageSpan = languages.AsSpan(0, 2);
languages[0] = "R";
Assert(languages[0] == languageSpan[0]);
Assert("R" == languageSpan[0]);
languageSpan[0] = "Lisp";
Assert(languages[0] == languageSpan[0]);
Assert("Lisp" == languages[0]);

int[] numbers = languages.Select(item => item.Length).ToArray();
// Create a Span<string> from the arrays first 3 elements.
Span<int> numbersSpan = numbers.AsSpan(0, 2);
Assert(numbers[1] == numbersSpan[1]);
numbersSpan[1] = 42;
Assert(numbers[1] == numbersSpan[1]);
Assert(42 == numbers[1]);

const string bigWord = "supercalifragilisticexpialidocious";
// Create a Span<char> from a suffix portion of the word.
#if NET8_0_OR_GREATER
ReadOnlySpan<char> expialidocious = bigWord.AsSpan(20..);
#else // NET8_0_OR_GREATER
ReadOnlySpan<char> expialidocious = bigWord.AsSpan(20, 14);
#endif // NET8_0_OR_GREATER
Assert(expialidocious.ToString() == "expialidocious");
#endregion INCLUDE
}

static void Assert(bool condition,
[CallerArgumentExpression(nameof(condition))]string expression = null!)
{
if (!condition)
{
throw new Exception($"Assertion failed: {expression}");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_09;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_10;

using System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_10;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_11;

using System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_10;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_11;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_12;

#region INCLUDE
using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_12;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_13;

#region INCLUDE
using System.Collections;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_13;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_14;

#region INCLUDE
using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_14;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_15;

using System;
using Listing17_10;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_15;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_16;

using System;
using Listing17_14;
using Listing17_15;

public class Program
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_16;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_17;

using System.Collections.Generic;
using Listing17_14;
using Listing17_15;
#region INCLUDE
public class BinaryTree<T> :
IEnumerable<T>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_17;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_18;

using System;
using Listing17_14;
using Listing17_16;
using Listing17_15;
using Listing17_17;

public class Program
{
Expand Down Expand Up @@ -38,4 +38,4 @@ public static void Main()
#endregion HIGHLIGHT
#endregion INCLUDE
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_18;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_19;

using System;
using Listing17_10;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_10;
using AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_11;
#region INCLUDE
using System;
using System.Collections;
using System.Collections.Generic;
#region EXCLUDE
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_19
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_20
{
#endregion EXCLUDE
[NullableContext(1)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_20;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_21;

using System;
using Listing17_10;
Expand Down
Loading