Warnings through Chapter10
MODIFY
src/Chapter07/Chapter07.csproj
src/Chapter07/Chapter07.csproj
<ChapterNumber>07</ChapterNumber>
</PropertyGroup>
<PropertyGroup>
+ <!--Listing the warning here causes it to be a warning rather than an error.-->
<!--CS0108 hides inherited member warning where 'new' is advised.-->
<ExpectedWarnings>$(ExpectedWarnings)CS0108,</ExpectedWarnings>
- <!--Listing the warning here causes it to be a warning rather than an error.-->
<IgnoredWarnings>$(IgnoredWarnings)</IgnoredWarnings>
</PropertyGroup>
<Import Project="..\Chapter.props" />

MODIFY
src/Chapter08.Tests/UndersandingAccessModifiers.Tests.cs
src/Chapter08.Tests/UndersandingAccessModifiers.Tests.cs
@@ -14,7 +14,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter08.UnderstandingAccessM
string InterfaceMethod1();
string InterfaceMethod2();
public string InterfaceMethod3();
- private string PrivateConcreteInterfaceMethod() => Thing.GetStackTrace();
+ private static string PrivateConcreteInterfaceMethod() => Thing.GetStackTrace();
protected string ProtectedConcreteInterfaceMethod() => Thing.GetStackTrace();
protected string ProtectedInterfaceMethod();
@@ -64,7 +64,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter08.UnderstandingAccessM
}
public class SubClass : Information
{
- new public string PublicConcreteInterfaceMethod() => Thing.GetStackTrace();
+ public new static string PublicConcreteInterfaceMethod() => Thing.GetStackTrace();
}
#pragma warning restore IDE0051 // Remove unused private members
@@ -148,7 +148,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter08.UnderstandingAccessM
SubClass information = new SubClass();
Assert.AreEqual(
$"{nameof(SubClass)}.{nameof(SubClass.PublicConcreteInterfaceMethod)}",
- information.PublicConcreteInterfaceMethod());
+ SubClass.PublicConcreteInterfaceMethod());
}
[TestMethod]

MODIFY
src/Chapter08/Listing08.13.CarelesslyRelyingOnAVirtualMethodImplementation.cs
src/Chapter08/Listing08.13.CarelesslyRelyingOnAVirtualMethodImplementation.cs
@@ -2,17 +2,17 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter08.Listing08_13
{
public class WorkflowActivity
{
- private void Start()
+ private static void Start()
{
// Critical code
}
public virtual void Run()
{
- Start();
+ WorkflowActivity.Start();
// Do something...
- Stop();
+ WorkflowActivity.Stop();
}
- private void Stop()
+ private static void Stop()
{
// Critical code
}

MODIFY
src/Chapter08/Listing08.14.ForcingTheDesirableRunEncapsulation.cs
src/Chapter08/Listing08.14.ForcingTheDesirableRunEncapsulation.cs
@@ -5,7 +5,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter08.Listing08_14
public interface IWorkflowActivity
{
// Private and, therefore, not virtual
- private void Start() =>
+ private static void Start() =>
Console.WriteLine(
"IWorkflowActivity.Start()...");
@@ -14,19 +14,19 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter08.Listing08_14
{
try
{
- Start();
+ IWorkflowActivity.Start();
InternalRun();
}
finally
{
- Stop();
+ IWorkflowActivity.Stop();
}
}
protected void InternalRun();
// Private and, therefore, not virtual
- private void Stop() =>
+ private static void Stop() =>
Console.WriteLine(
"IWorkflowActivity.Stop()...");
}
@@ -67,7 +67,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter08.Listing08_14
Console.WriteLine(
$"ExecuteProcessActivity.IExecuteProcessActivity.ExecuteProcess()...");
- public void Run()
+ public static void Run()
{
ExecuteProcessActivity activity
= new ExecuteProcessActivity("dotnet");
@@ -107,7 +107,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter08.Listing08_14
Console.WriteLine();
Console.WriteLine(
"Invoking activity.Run()...");
- activity.Run();
+ ExecuteProcessActivity.Run();
}
}
}

MODIFY
src/Chapter08/Table08.01.DefaultImplementedInterfaceMembers.cs
src/Chapter08/Table08.01.DefaultImplementedInterfaceMembers.cs
@@ -7,9 +7,14 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter08.Table08_01
{
public interface ISampleInterface
{
- static string? _Field;
- public static string? Field { get => _Field; private set => _Field = value; }
- static ISampleInterface() => Field = "Nelson Mandela";
+ private static string? _Field;
+ public static string? Field
+ {
+ get => _Field;
+ private set => _Field = value;
+ }
+ static ISampleInterface() =>
+ Field = "Nelson Mandela";
public static string? GetField() => Field;
}
}
@@ -194,7 +199,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter08.Table08_01
public interface IWorkflowActivity
{
// Private and, therefore, not virtual
- private void Start() =>
+ private static void Start() =>
Console.WriteLine(
"IWorkflowActivity.Start()...");
@@ -203,19 +208,19 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter08.Table08_01
{
try
{
- Start();
+ IWorkflowActivity.Start();
InternalRun();
}
finally
{
- Stop();
+ IWorkflowActivity.Stop();
}
}
protected void InternalRun();
// Private and, therefore, not virtual
- private void Stop() =>
+ private static void Stop() =>
Console.WriteLine(
"IWorkflowActivity.Stop()..");
}
@@ -250,12 +255,12 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter08.Table08_01
Value = value;
}
- partial void AssertValueIsValid(string value);
+ static partial void AssertValueIsValid(string value);
}
public partial interface IThing
{
- partial void AssertValueIsValid(string value)
+ static partial void AssertValueIsValid(string value)
{
// Throw if value is invalid.
switch (value)

MODIFY
src/Chapter09/Listing09.08.ComparingAnIntegerSwitchToAnEnumSwitch.cs
src/Chapter09/Listing09.08.ComparingAnIntegerSwitchToAnEnumSwitch.cs
@@ -4,7 +4,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_08
public class Program
{
- public void SwitchInt()
+ public static void SwitchInt()
{
int connectionState;
// ...
@@ -29,7 +29,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_08
}
- public void SwitchEnum()
+ public static void SwitchEnum()
{
ConnectionState connectionState;
// ...

MODIFY
src/Chapter10.Tests/CustomTestAttributes.cs
src/Chapter10.Tests/CustomTestAttributes.cs
@@ -16,7 +16,7 @@ namespace Chapter10.Tests.CustomTestAttributes
{
public override TestResult[] Execute(ITestMethod testMethod)
{
- var ignoreAttributes = FindAttributes(testMethod);
+ var ignoreAttributes = TestMethodWithIgnoreIfSupportAttribute.FindAttributes(testMethod);
// Evaluate each attribute, and skip if one returns `true`
foreach (var ignoreAttribute in ignoreAttributes)
@@ -37,7 +37,7 @@ namespace Chapter10.Tests.CustomTestAttributes
return base.Execute(testMethod);
}
- private IEnumerable<IgnoreIfAttribute> FindAttributes(ITestMethod testMethod)
+ private static IEnumerable<IgnoreIfAttribute> FindAttributes(ITestMethod testMethod)
{
// Look for an [IgnoreIf] on the method, including any virtuals this method overrides
var ignoreAttributes = new List<IgnoreIfAttribute>();

MODIFY
src/Chapter10/Chapter10.csproj
src/Chapter10/Chapter10.csproj
<PropertyGroup>
<ProductName>Chapter10</ProductName>
</PropertyGroup>
+ <PropertyGroup>
+ <!--Listing the warning here causes it to be a warning rather than an error.-->
+ <!--CA2013 Do not use ReferenceEquals with value types, and will always return false.-->
+ <ExpectedWarnings>$(ExpectedWarnings)CA2013,</ExpectedWarnings>
+ <IgnoredWarnings>$(IgnoredWarnings)</IgnoredWarnings>
+ </PropertyGroup>
<Import Project="..\Chapter.props" />
<ItemGroup>
<Compile Include="..\Shared\Program.cs">

MODIFY
src/Chapter10/Listing10.05.OverridingEquals.cs
src/Chapter10/Listing10.05.OverridingEquals.cs
@@ -13,17 +13,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_05
Coordinate coordinate1 =
new Coordinate(new Longitude(48, 52),
new Latitude(-2, -20));
-
- // Value types will never be reference equal
- if(Coordinate.ReferenceEquals(coordinate1,
- coordinate1))
- {
- throw new Exception(
- "coordinate1 reference equals coordinate1");
- }
-
- Console.WriteLine(
- "coordinate1 does NOT reference equal itself");
}
}
@@ -60,7 +49,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_05
{
// STEP 1: Check for null if a reference type
// (e.g., a reference type)
- // if (obj == null)
+ // if (ReferenceEquals(obj, null)
// {
// return false;
// }
@@ -68,14 +57,12 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_05
// STEP 4: Possibly check for equivalent hash codes
// but not if the identity properties are mutable
// and the hash code is cached.
- // if (this.GetHashCode() != obj.GetHashCode())
+ // if (GetHashCode() != obj.GetHashCode())
// {
// return false;
// }
// STEP 5: Check base.Equals if base overrides Equals().
- // System.Diagnostics.Debug.Assert(
- // base.GetType() != typeof(object) );
if (!base.Equals(obj))
{
return false;

MODIFY
src/Chapter10/Listing10.23.RegisteringAFinalizerWithProcessExit.cs
src/Chapter10/Listing10.23.RegisteringAFinalizerWithProcessExit.cs
WriteLine("Exiting...");
}
+ #pragma warning disable CA1816
#region IDisposable Members
public void Dispose()
{
Dispose(true);
}
#endregion
+ #pragma warning restore CA1816
public void Dispose(bool disposing)
{
WriteLine("Starting...");
WriteLine("Disposing managed stuff...");
// Unregister from the finalization queue.
- System.GC.SuppressFinalize(this);
+ GC.SuppressFinalize(this);
}
AppDomain.CurrentDomain.ProcessExit -=

Warnings through Chapter10