示例#1
0
文件: Car.cs 项目: MartiHr/Softuni
        public override string ToString()
        {
            string model  = $"{Model}:";
            string engine = Engine.ToString();
            string weight;
            string color;

            if (Weight != 0)
            {
                weight = $"  Weight: {Weight}";
            }
            else
            {
                weight = $"  Weight: n/a";
            }

            if (Color != null)
            {
                color = $"  Color: {Color}";
            }
            else
            {
                color = $"  Color: n/a";
            }

            return($"{model}\n{engine}\n{weight}\n{color}");
        }
        public override string ToString()
        {
            var sb = new StringBuilder();

            sb.Append($"{CarModel}:");
            sb.AppendLine();
            sb.Append($"  {Engine.ToString()}");
            sb.AppendLine();
            sb.Append(Weight == 0 ? "  Weight: n/a" : $"  Weight: {Weight}");
            sb.AppendLine();
            sb.Append(String.IsNullOrEmpty(Color) ? "  Color: n/a" : $"  Color: {Color}");

            return(sb.ToString().TrimEnd());
        }