public string ToString(ToStringFormat format) { StringBuilder sb = new StringBuilder(); string delimiter = "=="; string result = string.Empty; if (((int)format & (int)ToStringFormat.Name) == 1) { sb.Append(Name).Append(delimiter); } if (((int)format & (int)ToStringFormat.Phone) == 2) { sb.Append(ContactPhone).Append(delimiter); } if (((int)format & (int)ToStringFormat.Revenue) == 4) { sb.Append(string.Format("{0:N2}", Revenue)).Append(delimiter); } result = sb.ToString(); if (result.Split(delimiter.ToCharArray()).Length > 1) { return(result.Replace(delimiter, ", ").TrimEnd(", ".ToCharArray())); } else { return(result.Replace(delimiter, string.Empty)); } }
/// <returns><see cref="object.ToString()"/> or the return value of <see cref="StringFormat"/> when provided.</returns> public override string ToString() { return($"{ToStringFormat?.Invoke(this, GetFormattedName(), GetFormattedValue()) ?? $"{GetFormattedName()}: {GetFormattedValue()}"}[]"); }
public string Customer_ToString_Test(string name, string phone, decimal revenue, ToStringFormat format) { Customer customer = new Customer(name, phone, revenue); return(customer.ToString(format)); }