Refactoring and Nullability
MODIFY
src/Chapter08/Listing08.02.ImplementingAndUsingInterfaces.cs
src/Chapter08/Listing08.02.ImplementingAndUsingInterfaces.cs
public interface IListable
{
- // Return the value of each column in the row
- string[] ColumnValues
+ // Return the value of each cell in the row
+ string?[] CellValues
{
get;
}
public class Contact : PdaItem, IListable
{
- // Non-nullable field is uninitialized. Consider declaring as nullable.
-#pragma warning disable CS8618 // Disabled because the properties are set via the based classes
- // call to the virtual Name property.
public Contact(string firstName, string lastName,
string address, string phone)
: base(GetName(firstName, lastName))
{
- // FirstName and LastName are set via the based classes
- // call to the virtual Name property.
- // FirstName = firstName;
- // LastName = lastName;
+ FirstName = firstName;
+ LastName = lastName;
Address = address;
Phone = phone;
}
-#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Address { get; set; }
- public string Phone { get; set; }
+ public string FirstName { get; }
+ public string LastName { get; }
+ public string Address { get; }
+ public string Phone { get; }
- static string GetName(string firstName, string lastName) => $"{ firstName } { lastName }";
- override public string Name
- {
- get
- {
- return GetName(FirstName, LastName);
- }
- set
- {
- // Split the assigned value into
- // first and last names
- string[] names;
- names = value.Split(new char[] { ' ' });
- if (names.Length == 2)
- {
- FirstName = names[0];
- LastName = names[1];
- }
- else
- {
- // Throw an exception if the full
- // name was not assigned
- throw new System.ArgumentException(
- $"Assigned value '{ value }' is invalid",
- "value");
- }
- }
- }
-
- public string[] ColumnValues
+ public string[] CellValues
{
get
{
}
}
- // ...
+ static public string GetName(string firstName, string lastName)
+ => $"{ firstName } { lastName }";
}
public class Publication : IListable
Year = year;
}
- public string Title { get; set; }
- public string Author { get; set; }
- public int Year { get; set; }
+ public string Title { get; }
+ public string Author { get; }
+ public int Year { get; }
- public string[] ColumnValues
+ public string?[] CellValues
{
get
{
- return new string[]
+ return new string?[]
{
Title,
Author,
for(int count = 0; count < items.Length; count++)
{
- string[] values = items[count].ColumnValues;
+ string?[] values = items[count].CellValues;
DisplayItemRow(columnWidths, values);
}
}
}
private static void DisplayItemRow(
- int[] columnWidths, string[] values)
+ int[] columnWidths, string?[] values)
{
if(columnWidths.Length != values.Length)
{
for(int index = 0; index < values.Length; index++)
{
- string itemToPrint = values[index].PadRight(columnWidths[index], ' ');
+ string itemToPrint = (values[index]??"").PadRight(columnWidths[index], ' ');
Console.Write(itemToPrint);
}
Console.WriteLine();

Refactoring and Nullability