Skip to content
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
15 changes: 2 additions & 13 deletions collections-csharp/IEnumerableCSharp/IEnumerableCSharp/Book.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
namespace IEnumerableCSharp
{
public class Book
{
public Book(string name, string author)
{
Name = name;
Author = author;
}
namespace IEnumerableCSharp;

public string Name { get; set; }
public string Author { get; set; }
}
}
public record Book(string Name, string Author);
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
using System.Collections;

namespace IEnumerableCSharp
namespace IEnumerableCSharp;

public class BookEnumerator : IEnumerator
{
public class BookEnumerator : IEnumerator
{
private readonly Book[] _books;
private int _index = -1;
private readonly Book[] _books;
private int _index = -1;

public BookEnumerator(Book[] books)
{
_books = books;
}
public BookEnumerator(Book[] books)
{
_books = books;
}

public Book Current
public Book Current
{
get
{
get
{
return _books[_index];
}
return _books[_index];
}
}

object IEnumerator.Current => Current;
object IEnumerator.Current => Current;

public bool MoveNext()
{
_index++;
return _index < _books.Length;
}
public bool MoveNext()
{
_index++;
return _index < _books.Length;
}

public void Reset()
{
_index = -1;
}
public void Reset()
{
_index = -1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections;

namespace IEnumerableCSharp;

public class GenericLibrary : IEnumerable<Book>
{
private readonly Book[] _books;

public GenericLibrary(Book[] books)
{
_books = [.. books];
}

public IEnumerator<Book> GetEnumerator()
{
foreach (var book in _books)
{
yield return book;
}
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
27 changes: 10 additions & 17 deletions collections-csharp/IEnumerableCSharp/IEnumerableCSharp/Library.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
using System.Collections;

namespace IEnumerableCSharp
namespace IEnumerableCSharp;

public class Library : IEnumerable
{
public class Library : IEnumerable
{
private readonly Book[] _books;
private readonly Book[] _books;

public Library(Book[] books)
{
_books = new Book[books.Length];
public Library(Book[] books)
{
_books = [.. books];
}

for (int i = 0; i < books.Length; i++)
{
_books[i] = books[i];
}
}
public BookEnumerator GetEnumerator() => new BookEnumerator(_books);

public BookEnumerator GetEnumerator() => new BookEnumerator(_books);

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
44 changes: 23 additions & 21 deletions collections-csharp/IEnumerableCSharp/IEnumerableCSharp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
namespace IEnumerableCSharp
using IEnumerableCSharp;

Book[] books =
[
new("Anna Karenina", "Leo Tolstoy"),
new("To Kill a Mockingbird", "Harper Lee"),
new("The Great Gatsby", "F. Scott Fitzgerald"),
new("One Hundred Years of Solitude", "Gabriel García Márquez"),
new("A Passage to India", "E.M. Forster")
];

var library = new Library(books);

foreach (var book in library)
{
internal class Program
{
static void Main(string[] args)
{
var books = new Book[5]
{
new Book("Anna Karenina", "Leo Tolstoy"),
new Book("To Kill a Mockingbird", "Harper Lee"),
new Book("The Great Gatsby", "F. Scott Fitzgerald"),
new Book("One Hundred Years of Solitude", "Gabriel García Márquez"),
new Book("A Passage to India", "E.M. Forster")
};
Console.WriteLine($"Book: {book.Name}, Author: {book.Author}");
}

var library = new Library(books);
var genericLibrary = new GenericLibrary(books);

foreach (var book in library)
{
Console.WriteLine($"Book: {book.Name}, Author: {book.Author}");
}
}
}
}
var tolstoy = genericLibrary.Where(book => book.Author == "Leo Tolstoy");

foreach (var book in tolstoy)
{
Console.WriteLine($"By Tolstoy: {book.Name}");
}
51 changes: 51 additions & 0 deletions collections-csharp/IEnumerableCSharp/Tests/LibraryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections;
using IEnumerableCSharp;

namespace Tests;

public class LibraryTests
{
private static readonly Book[] Books =
[
new("Anna Karenina", "Leo Tolstoy"),
new("To Kill a Mockingbird", "Harper Lee"),
new("War and Peace", "Leo Tolstoy")
];

[Fact]
public void WhenIteratingLibrary_ThenEveryBookComesBackInOrder()
{
var names = new List<string>();

foreach (var book in new Library(Books))
{
names.Add(book.Name);
}

Assert.Equal(["Anna Karenina", "To Kill a Mockingbird", "War and Peace"], names);
}

[Fact]
public void WhenUsingLinqOnNonGenericLibrary_ThenCastIsNeeded()
{
var tolstoy = new Library(Books).Cast<Book>().Where(book => book.Author == "Leo Tolstoy");

Assert.Equal(2, tolstoy.Count());
}

[Fact]
public void WhenUsingLinqOnGenericLibrary_ThenNoCastIsNeeded()
{
var tolstoy = new GenericLibrary(Books).Where(book => book.Author == "Leo Tolstoy");

Assert.Equal(2, tolstoy.Count());
}

[Fact]
public void WhenResettingIteratorEnumerator_ThenNotSupportedExceptionIsThrown()
{
using var enumerator = new GenericLibrary(Books).GetEnumerator();

Assert.Throws<NotSupportedException>(() => enumerator.Reset());
}
}
14 changes: 9 additions & 5 deletions collections-csharp/IEnumerableCSharp/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../IEnumerableCSharp/IEnumerableCSharp.csproj" />
</ItemGroup>

</Project>
Loading