Warnings through Chapter 23
MODIFY
src/Chapter19.Tests/SharedProgram.Tests.cs
src/Chapter19.Tests/SharedProgram.Tests.cs
@@ -49,7 +49,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
public async Task InvokeMethodUsingReflection(string methodName, string[]? args, string? expected)
{
Assert.AreEqual<string?>(expected,
- await Program.InvokeMethodUsingReflection(
+ await Program.InvokeMethodUsingReflectionAsync(
typeof(SampleClass).GetMethod(methodName)!, args));
}
}

MODIFY
src/Chapter21/Listing21.06.LinqSelect.cs
src/Chapter21/Listing21.06.LinqSelect.cs
@@ -8,16 +8,16 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter21.Listing21_06
class Program
{
// ...
- public List<string>
+ public static List<string>
Encrypt(IEnumerable<string> data)
{
return data.Select(
- item => Encrypt(item)).ToList();
+ item => Program.Encrypt(item)).ToList();
}
// ...
- private string Encrypt(string item)
+ private static string Encrypt(string item)
{
Console.WriteLine($">>>>>Encrypting '{ item }'.");
Cryptographer cryptographer = new Cryptographer();

MODIFY
src/Chapter21/Listing21.07.ParallelLinqSelect.cs
src/Chapter21/Listing21.07.ParallelLinqSelect.cs
@@ -8,16 +8,16 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter21.Listing21_07
class Program
{
// ...
- public List<string>
+ public static List<string>
Encrypt(IEnumerable<string> data)
{
return data.AsParallel().Select(
- item => Encrypt(item)).ToList();
+ item => Program.Encrypt(item)).ToList();
}
// ...
- private string Encrypt(string item)
+ private static string Encrypt(string item)
{
Console.WriteLine($">>>>>Encrypting '{ item }'.");
Cryptographer cryptographer = new Cryptographer();

MODIFY
src/Chapter21/Listing21.08.ParallelLinqStandardQueryOperators.cs
src/Chapter21/Listing21.08.ParallelLinqStandardQueryOperators.cs
@@ -8,7 +8,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter21.Listing21_08
class Program
{
// ...
- public List<string>
+ public static List<string>
Encrypt(IEnumerable<string> data)
{
@@ -18,19 +18,19 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter21.Listing21_08
// Show the total count of items still
// matches the original count
if (data.Count() != parallelGroups.Sum(
- item => item.Count()))
+ item => item.Length))
{
throw new Exception("data.Count() != parallelGroups.Sum(item => item.Count()");
}
// ...
return data.AsParallel().Select(
- item => Encrypt(item)).ToList();
+ item => Program.Encrypt(item)).ToList();
}
// ...
- private string Encrypt(string item)
+ private static string Encrypt(string item)
{
Console.WriteLine($">>>>>Encrypting '{ item }'.");
Cryptographer cryptographer = new Cryptographer();

MODIFY
src/Chapter21/Listing21.09.ParallelLinqQueryExpressions.cs
src/Chapter21/Listing21.09.ParallelLinqQueryExpressions.cs
@@ -8,7 +8,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter21.Listing21_09
class Program
{
// ...
- public List<string>
+ public static List<string>
Encrypt(IEnumerable<string> data)
{
@@ -28,12 +28,12 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter21.Listing21_09
// ...
return data.AsParallel().Select(
- item => Encrypt(item)).ToList();
+ item => Program.Encrypt(item)).ToList();
}
// ...
- private string Encrypt(string item)
+ private static string Encrypt(string item)
{
Console.WriteLine($">>>>>Encrypting '{ item }'.");
Cryptographer cryptographer = new Cryptographer();

MODIFY
src/Chapter22/Listing22.02.UnsychronizedLocalVariables.cs
src/Chapter22/Listing22.02.UnsychronizedLocalVariables.cs
@@ -6,8 +6,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter22.Listing22_02
public class Program
{
- static public CancellationToken CancellationToken;
-
public static int Main(string[] args)
{
int total = int.MaxValue;
@@ -15,9 +13,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter22.Listing22_02
if (args?.Length > 0) { _ = int.TryParse(args[0], out total); }
Console.WriteLine($"Increment and decrementing {total} times...");
- Parallel.For(0, total,
- new ParallelOptions() { CancellationToken = CancellationToken}
- , i =>
+ Parallel.For(0, total, i =>
{
x++;
x--;

MODIFY
src/Chapter22/Listing22.13.TaskDelayAsTimer.cs
src/Chapter22/Listing22.13.TaskDelayAsTimer.cs
@@ -15,7 +15,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter22.Listing22_13
DisplayMinuteTicker(minute);
for(int second = 0; second < 60; second++)
{
- await Task.Delay(1000);
+ await Task.Delay(1000, token);
if(token.IsCancellationRequested)
break;
DisplaySecondTicker();

MODIFY
src/Chapter22/PiCalculation.cs
src/Chapter22/PiCalculation.cs
@@ -17,12 +17,12 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter22
int digits, object? userState)
{
CalculateAsync(
- digits, default, userState);
+ digits, userState, default);
}
public void CalculateAsync<TState>(
int digits,
- CancellationToken cancelToken,
- TState userState)
+ TState userState,
+ CancellationToken cancelToken)
{
Task<string>.Factory.StartNew(
() => PiCalculator.Calculate(digits), cancelToken)

MODIFY
src/Chapter23/Listing23.20.DesignatingABlockForUnsafeCode.cs
src/Chapter23/Listing23.20.DesignatingABlockForUnsafeCode.cs
@@ -54,7 +54,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter23.Listing23_20
}
}
- public class VirtualMemoryPtr : System.Runtime.InteropServices.SafeHandle
+ public class VirtualMemoryPtr : System.Runtime.InteropServices.SafeHandle, IDisposable
{
public VirtualMemoryPtr(int memorySize) :
base(IntPtr.Zero, true)
@@ -90,15 +90,37 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter23.Listing23_20
// SafeHandle abstract member
protected override bool ReleaseHandle()
+ {
+ Dispose(disposing: false);
+ return true;
+ }
+
+ protected override void Dispose(bool disposing)
{
if (!Disposed)
+ {
+ base.Dispose(disposing);
+ if (!disposing)
{
Disposed = true;
- GC.SuppressFinalize(this);
VirtualMemoryManager.VirtualFreeEx(ProcessHandle,
AllocatedPointer, MemorySize);
}
- return true;
+ Disposed = true;
+ }
+ }
+
+ ~VirtualMemoryPtr()
+ {
+ // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
+ Dispose(disposing: false);
+ }
+
+ void IDisposable.Dispose()
+ {
+ // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
+ Dispose(disposing: true);
+ GC.SuppressFinalize(this);
}
}
@@ -147,8 +169,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter23.Listing23_20
GetCurrentProcessHandle(), lpAddress, dwSize);
}
-
-
/// <summary>
/// The type of memory allocation. This parameter must
/// contain one of the following values.

MODIFY
src/Shared/NetCore.cs
src/Shared/NetCore.cs
using System;
using System.Reflection;
-#nullable enable
+
+namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
+{
public static class NetCore
{
public static string GetNetCoreVersion()
@@ -13,3 +15,4 @@ public static class NetCore
throw new Exception("Unable to determine .NET Core version.");
}
}
+}

Warnings through Chapter 23