Through 12.30
MODIFY
src/Chapter12/Listing12.24.DeclaringAGenericWithAMulticastDelegateConstraint.cs
src/Chapter12/Listing12.24.DeclaringAGenericWithAMulticastDelegateConstraint.cs
@@ -4,6 +4,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_31
{
public class Publisher
{
+ #region INCLUDE
static public object? InvokeAll<TDelegate>(
object?[]? args, params TDelegate[] delegates)
// Constraint of type Action/Func not allowed
@@ -20,6 +21,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_31
return null;
};
}
+ #endregion INCLUDE
static public void InvokeAll(params Action?[] actions)
{

MODIFY
src/Chapter12/Listing12.25.SpecifyingTheTypeParameterAsAValueType.cs
src/Chapter12/Listing12.25.SpecifyingTheTypeParameterAsAValueType.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_24
{
using System;
-
+ #region INCLUDE
public struct Nullable<T> :
IFormattable, IComparable,
IComparable<Nullable<T>>, INullable
+ #region HIGHLIGHT
where T : struct
+ #endregion HIGHLIGHT
+ {
+ // ...
+ public static implicit operator Nullable<T>(T value) =>
+ new Nullable<T>(value);
+ public static explicit operator T(Nullable<T> value) => value!.Value;
+ #region EXCLUDE
+ public Nullable(T value)
{
// ...
+ }
+ public T Value => throw new NotImplementedException();
public string ToString(string? format, IFormatProvider? formatProvider)
{
throw new NotImplementedException();
@@ -27,6 +38,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_24
{
get { throw new NotImplementedException(); }
}
+ #endregion EXCLUDE
}
-
+ #endregion INCLUDE
}

MODIFY
src/Chapter12/Listing12.26.SpecifyingMultipleConstraints.cs
src/Chapter12/Listing12.26.SpecifyingMultipleConstraints.cs
@@ -3,7 +3,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_25
using System;
using System.Collections.Generic;
using Listing12_23;
-
+ #region INCLUDE
public class EntityDictionary<TKey, TValue>
: Dictionary<TKey, TValue>
where TKey : IComparable<TKey>, IFormattable
@@ -11,4 +11,5 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_25
{
// ...
}
+ #endregion INCLUDE
}

MODIFY
src/Chapter12/Listing12.27.RequiringADefaultConstructorConstraint.cs
src/Chapter12/Listing12.27.RequiringADefaultConstructorConstraint.cs
@@ -2,29 +2,31 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_26
{
using System;
using System.Collections.Generic;
-
+ #region INCLUDE
public class EntityBase<TKey>
where TKey: notnull
{
- public TKey Key { get; set; }
-
public EntityBase(TKey key)
{
Key = key;
}
+
+ public TKey Key { get; set; }
}
public class EntityDictionary<TKey, TValue> :
Dictionary<TKey, TValue>
where TKey : IComparable<TKey>, IFormattable
+ #region HIGHLIGHT
where TValue : EntityBase<TKey>, new()
+ #endregion HIGHLIGHT
{
- // ...
-
public TValue MakeValue(TKey key)
{
+ #region HIGHLIGHT
TValue newEntity = new TValue
{
+ #endregion HIGHLIGHT
Key = key
};
Add(newEntity.Key, newEntity);
@@ -33,4 +35,5 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_26
// ...
}
+ #endregion INCLUDE
}

MODIFY
src/Chapter12/Listing12.28.ConstraintsCanOnlyBeSpecifiedForDefaultConstructors.cs
src/Chapter12/Listing12.28.ConstraintsCanOnlyBeSpecifiedForDefaultConstructors.cs
@@ -2,14 +2,18 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_28
{
public class Program
{
- //public TValue New(TKey key)
- //{
- // // Error: 'TValue': Cannot provide arguments
- // // when creating an instance of a variable type
- // TValue newEntity = null;
- // // newEntity = new TValue(key);
- // Add(newEntity.Key, newEntity);
- // return newEntity;
- //}
+ /*
+ #region INCLUDE
+ public TValue New(TKey key)
+ {
+ // Error: 'TValue': Cannot provide arguments
+ // when creating an instance of a variable type
+ TValue newEntity = null;
+ // newEntity = new TValue(key);
+ Add(newEntity.Key, newEntity);
+ return newEntity;
+ }
+ #endregion INCLUDE
+ */
}
}

MODIFY
src/Chapter12/Listing12.29.UsingAFactoryInterfaceInPlaceOfAConstructorConstraint.cs
src/Chapter12/Listing12.29.UsingAFactoryInterfaceInPlaceOfAConstructorConstraint.cs
@@ -3,35 +3,45 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_29
using System.Collections.Generic;
using System;
+ #region INCLUDE
public class EntityBase<TKey>
{
+ #region HIGHLIGHT
public EntityBase(TKey key)
{
Key = key;
}
+ #endregion HIGHLIGHT
public TKey Key { get; set; }
}
- public interface IEntityFactory<TKey, TValue>
- {
- TValue CreateNew(TKey key);
- }
-
public class EntityDictionary<TKey, TValue, TFactory> :
Dictionary<TKey, TValue>
where TKey : IComparable<TKey>, IFormattable
+ #region HIGHLIGHT
where TValue : EntityBase<TKey>
where TFactory : IEntityFactory<TKey, TValue>, new()
+ #endregion HIGHLIGHT
{
// ...
public TValue New(TKey key)
{
TFactory factory = new TFactory();
+ #region HIGHLIGHT
TValue newEntity = factory.CreateNew(key);
+ #endregion HIGHLIGHT
Add(newEntity.Key, newEntity);
return newEntity;
}
//...
}
+
+ #region HIGHLIGHT
+ public interface IEntityFactory<TKey, TValue>
+ {
+ TValue CreateNew(TKey key);
+ }
+ #endregion HIGHLIGHT
+ #endregion INCLUDE
}

MODIFY
src/Chapter12/Listing12.30.DeclaringAnEntityToBeUsedInEntityDictionary.cs
src/Chapter12/Listing12.30.DeclaringAnEntityToBeUsedInEntityDictionary.cs
@@ -2,7 +2,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_30
{
using System;
using Listing12_29;
-
+ #region INCLUDE
public class Order : EntityBase<Guid>
{
public Order(Guid key) :
@@ -19,4 +19,5 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_30
return new Order(key);
}
}
+ #endregion INCLUDE
}

Through 12.30