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
1 change: 0 additions & 1 deletion src/Chapter17/Chapter17.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<Compile Remove="Listing17.19.CSharpEquivalentOfCompilerGeneratedCodeForIterators.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Listing17.11.SpecifyingAReturnAttribute.Description.cs" />
<None Include="Listing17.19.CSharpEquivalentOfCompilerGeneratedCodeForIterators.cs" />
<Compile Include="..\Shared\Program.cs">
<Link>Program.cs</Link>
Expand Down
2 changes: 2 additions & 0 deletions src/Chapter17/Listing17.01.UsingList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_01
{
#region INCLUDE
using System;
using System.Collections.Generic;

Expand All @@ -19,4 +20,5 @@ public static void Main()
list.Remove("Grumpy");
}
}
#endregion INCLUDE
}
29 changes: 15 additions & 14 deletions src/Chapter17/Listing17.02.ImplementingIComparer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_02
{
#region INCLUDE
using System;
using System.Collections.Generic;

#region EXCLUDE
public class Program
{
public static void Main()
Expand Down Expand Up @@ -33,8 +34,19 @@ public static void Main()
}
}
}
#endregion EXCLUDE
public class Contact
{
public string FirstName { get; private set; }
public string LastName { get; private set; }

class NameComparison : IComparer<Contact>
public Contact(string firstName, string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}
}
public class NameComparison : IComparer<Contact>
{
public int Compare(Contact? x, Contact? y)
{
Expand All @@ -61,16 +73,5 @@ private static int StringCompare(string? x, string? y)
return x.CompareTo(y);
}
}

class Contact
{
public string FirstName { get; private set; }
public string LastName { get; private set; }

public Contact(string firstName, string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}
}
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_03
{
#region INCLUDE
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -28,4 +29,5 @@ public static void Main()
}
}
}
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_04
{
#region INCLUDE
using System;
using System.Collections.Generic;

Expand All @@ -13,23 +14,19 @@ public static void Main()
list.Add(3);
list.Add(2);

#region HIGHLIGHT
List<int> results = list.FindAll(Even);
#endregion HIGHLIGHT

foreach(int number in results)
#region HIGHLIGHT
foreach (int number in results)
#endregion HIGHLIGHT
{
Console.WriteLine(number);
}
}

#if !PRECSHARP6
public static bool Even(int value) =>
(value % 2) == 0;
#else
public static bool Even(int value)
{
return (value % 2) == 0;
}
#endif
}

#endregion INCLUDE
}
23 changes: 8 additions & 15 deletions src/Chapter17/Listing17.05.AddingItemsToADictionary.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_05
{
#region INCLUDE
using System;
using System.Collections.Generic;

public class Program
{
public static void Main()
{
#if !PRECSHARP5
// C# 6.0
Dictionary<string, ConsoleColor> colorMap =
new Dictionary<string, ConsoleColor>
// C# 6.0 (use {"Error", consoleColor.Red} pre-C# 6.0)
var colorMap = new Dictionary<string, ConsoleColor>
{
["Error"] = ConsoleColor.Red,
["Warning"] = ConsoleColor.Yellow,
["Information"] = ConsoleColor.Green
};
#else
// Pre-C# 6.0
Dictionary<string, ConsoleColor> colorMap =
new Dictionary<string, ConsoleColor>
{
{"Error", ConsoleColor.Red },
{"Warning", ConsoleColor.Yellow },
{"Information", ConsoleColor.Green }
};
#endif

#region HIGHLIGHT
colorMap.Add("Verbose", ConsoleColor.White);
#endregion HIGHLIGHT
#region EXCLUDE

Print(colorMap);
}
Expand All @@ -39,6 +30,8 @@ private static void Print(IEnumerable<KeyValuePair<string, ConsoleColor>> items)
Console.ForegroundColor = item.Value;
Console.WriteLine(item.Key);
}
#endregion EXCLUDE
}
}
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_06
{
#region INCLUDE
using System;
using System.Collections.Generic;

Expand All @@ -8,17 +9,19 @@ public class Program
public static void Main()
{
// C# 6.0 (use {"Error", ConsoleColor.Red} pre-C# 6.0)
Dictionary<string, ConsoleColor> colorMap =
new Dictionary<string, ConsoleColor>
var colorMap = new Dictionary<string, ConsoleColor>
{
["Error"] = ConsoleColor.Red,
["Warning"] = ConsoleColor.Yellow,
["Information"] = ConsoleColor.Green
};

#region HIGHLIGHT
colorMap["Verbose"] = ConsoleColor.White;
colorMap["Error"] = ConsoleColor.Cyan;
#endregion HIGHLIGHT

#region EXCLUDE
Print(colorMap);
}

Expand All @@ -29,6 +32,8 @@ private static void Print(IEnumerable<KeyValuePair<string, ConsoleColor>> items)
Console.ForegroundColor = item.Value;
Console.WriteLine(item.Key);
}
#endregion EXCLUDE
}
}
#endregion INCLUDE
}
31 changes: 13 additions & 18 deletions src/Chapter17/Listing17.07.IteratingOverDictionaryWithForeach.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_07
{
#region INCLUDE
using System;
using System.Collections.Generic;

public class Program
{
public static void Main()
{
#if !PRECSHARP5
Dictionary<string, ConsoleColor> colorMap =
new Dictionary<string, ConsoleColor>
// C# 6.0 (use {"Error", consoleColor.Red} pre-C# 6.0)
var colorMap = new Dictionary<string, ConsoleColor>
{
["Error"] = ConsoleColor.Red,
["Warning"] = ConsoleColor.Yellow,
["Information"] = ConsoleColor.Green,
["Verbose"] = ConsoleColor.White
};
#else
Dictionary<string, ConsoleColor> colorMap =
new Dictionary<string, ConsoleColor>
{
{"Error", ConsoleColor.Red },
{"Warning", ConsoleColor.Yellow },
{"Information", ConsoleColor.Green },
{"Verbose", ConsoleColor.White}
};
#endif

Print(colorMap);
}

private static void Print(
#region HIGHLIGHT
private static void Print(
IEnumerable<KeyValuePair<string, ConsoleColor>> items)
{
foreach (KeyValuePair<string, ConsoleColor> item in items)
{
Console.ForegroundColor = item.Value;
#endregion HIGHLIGHT
{
#region HIGHLIGHT
foreach (KeyValuePair<string, ConsoleColor> item in items)
#endregion HIGHLIGHT
{
Console.ForegroundColor = item.Value;
Console.WriteLine(item.Key);
}
}
}
#endregion INCLUDE
}

This file was deleted.

9 changes: 5 additions & 4 deletions src/Chapter17/Listing17.08.ImplementingIEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter17.Listing17_08
{
using System;
#region INCLUDE
using System.Collections.Generic;

class ContactEquality : IEqualityComparer<Contact>
public class ContactEquality : IEqualityComparer<Contact>
{
public bool Equals(Contact? x, Contact? y)
{
if(Object.ReferenceEquals(x, y))
if(object.ReferenceEquals(x, y))
return true;
if(x == null || y == null)
return false;
Expand All @@ -25,8 +25,9 @@ public int GetHashCode(Contact x)
return h1 * 23 + h2;
}
}
#endregion INCLUDE

class Contact
public class Contact
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
Expand Down
Loading