Addressed nullabilty in Chapter 17.
MODIFY
src/Chapter17/Listing17.19.CSharpEquivalentOfCompilerGeneratedCodeForIterators.cs
src/Chapter17/Listing17.19.CSharpEquivalentOfCompilerGeneratedCodeForIterators.cs
+using AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_10;
+using System;
using System.Collections;
+using System.Collections.Generic;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_19
{
- using Listing17_10;
- using System;
- using System.Collections.Generic;
-// Not concerned about Type naming for compiler simulated code
-#pragma warning disable CS0693
-
- public class Pair<T> : IPair<T>, IEnumerable<T>
+ [NullableContext(1)]
+ [Nullable(0)]
+ public struct Pair<[Nullable(2)] T> : IPair<T>, IEnumerable<T>, IEnumerable
{
- // ...
-
- // The iterator is expanded into the following
- // code by the compiler
-
- public virtual IEnumerator<T> GetEnumerator()
+ public Pair(T first, T second)
{
- __ListEnumerator<T> result = new __ListEnumerator<T>(0);
- result._Pair = this;
- return result;
+ First = first;
+ Second = second;
}
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
+ public T First { get; }
- public T First { get; private set; }
- public T Second { get; private set; }
+ public T Second { get; }
- #region Indexer
public T this[PairItem index]
{
get
{
- switch(index)
- {
- case PairItem.First:
- return First;
- case PairItem.Second:
- return Second;
- default:
- throw new NotImplementedException(
- string.Format(
- "The enum {0} has not been implemented",
- index.ToString()));
- }
- }
- }
- #endregion Indexer
-
- private sealed class __ListEnumerator<T> : IEnumerator<T>
+ PairItem pairItem = index;
+ PairItem pairItem2 = pairItem;
+ T result;
+ if (pairItem2 != PairItem.First)
{
- public __ListEnumerator(int itemCount)
+ if (pairItem2 != PairItem.Second)
{
- _ItemCount = itemCount;
+ throw new NotImplementedException(string.Format("The enum {0} has not been implemented", index.ToString()));
}
-
- public Pair<T> _Pair;
- T _Current;
- int _ItemCount;
-
- public void Reset()
- {
- throw new NotImplementedException();
+ result = Second;
}
-
- public T Current
+ else
{
- get { return _Current; }
+ result = First;
+ }
+ return result;
}
-
- object IEnumerator.Current
- {
- get { return Current; }
}
- public bool MoveNext()
+ public IEnumerator<T> GetEnumerator()
{
- switch(_ItemCount)
- {
- case 0:
- _Current = _Pair.First;
- _ItemCount++;
- return true;
- case 1:
- _Current = _Pair.Second;
- _ItemCount++;
- return true;
- default:
- return false;
- }
+ yield return First;
+ yield return Second;
+ yield break;
}
- public void Dispose()
+ IEnumerator IEnumerable.GetEnumerator()
{
- throw new NotImplementedException();
- }
+ return GetEnumerator();
}
}
-#pragma warning restore CS0693
}

Addressed nullabilty in Chapter 17.