I'm trying to work with the following class, which is inside a library I currently cannot change:
// This class represents "external" code as far as my review
// request is concerned: I cannot change this, currently.
public class AddressDto
{
public string Name { get; set; }
public string Zip { get; set; }
public string City { get; set; }
public string AddressLine { get; set; }
public string ExtraInfo { get; set; }
}
I'm writing an extension method with c# 5 and .NET 4.5.1 to represent instances of that class as a string for display purposes. I'm looking to get feedback on the inner workings of this method:
// Looking for feedback on this method:
public static string FormatForGui(this AddressDto Address)
{
if (Address == null) throw new ArgumentNullException("Address");
var parts = new[]
{
Address.Name,
Address.AddressLine,
Address.Zip,
Address.City
};
return string.Join(" ", parts.Where(s => !String.IsNullOrEmpty(s)));
}
Can this method body be improved?
For your convenience, here's a condensed NUnit unit test to define the behavior:
// Not necessarily looking for feedback on these tests, my actual tests are more DAMP
// but for brevity I condensed them into one test:
[Test]
public void AllTests()
{
Assert.Throws<ArgumentNullException>(() => ((AddressDto)null).FormatForGui());
Assert.That(new AddressDto { }.FormatForGui(), Is.EqualTo(""));
Assert.That(new AddressDto { Name = "" }.FormatForGui(), Is.EqualTo(""));
Assert.That(new AddressDto { Name = "A" }.FormatForGui(), Is.EqualTo("A"));
Assert.That(new AddressDto { Name = "A", AddressLine = "B" }.FormatForGui(), Is.EqualTo("A B"));
Assert.That(new AddressDto { Name = "A", AddressLine = "B", Zip = "C" }.FormatForGui(), Is.EqualTo("A B C"));
Assert.That(new AddressDto { Name = "A", AddressLine = "B", Zip = "C", City = "D" }.FormatForGui(), Is.EqualTo("A B C D"));
Assert.That(new AddressDto { Name = "A", AddressLine = "B", City = "D" }.FormatForGui(), Is.EqualTo("A B D"));
}
camelCase
casing (based on the NET naming guidelines). \$\endgroup\$