• C# IFormattable 接口重写


     1  public class Racer : IComparable<Racer>, IFormattable
     2   {
     3     public int Id { get; private set; }
     4     public string FirstName { get; set; }
     5     public string LastName { get; set; }
     6     public string Country { get; set; }
     7     public int Wins { get; set; }
     8 
     9     public Racer(int id, string firstName, string lastName, string country = null, int wins = 0)
    10     {
    11       this.Id = id;
    12       this.FirstName = firstName;
    13       this.LastName = lastName;
    14       this.Country = country;
    15       this.Wins = wins;
    16     }
    17 
    18     public override string ToString()
    19     {
    20       return String.Format("{0} {1}", FirstName, LastName);
    21     }
    22 
    23     public string ToString(string format, IFormatProvider formatProvider)
    24     {
    25       if (format == null) format = "N";
    26       switch (format.ToUpper())
    27       {
    28         case "N": // name
    29           return ToString();
    30         case "F": // first name
    31           return FirstName;
    32         case "L": // last name
    33           return LastName;
    34         case "W": // Wins
    35           return String.Format("{0}, Wins: {1}", ToString(), Wins);
    36         case "C": // Country
    37           return String.Format("{0}, Country: {1}", ToString(), Country);
    38         case "A": // All
    39           return String.Format("{0}, {1} Wins: {2}", ToString(), Country, Wins);
    40         default:
    41           throw new FormatException(String.Format(formatProvider,
    42                 "Format {0} is not supported", format));
    43       }
    44     }
    45 
    46     public string ToString(string format)
    47     {
    48       return ToString(format, null);
    49     }
    50 
    51     public int CompareTo(Racer other)
    52     {
    53       int compare = this.LastName.CompareTo(other.LastName);
    54       if (compare == 0)
    55         return this.FirstName.CompareTo(other.FirstName);
    56       return compare;
    57     }
    58   }
  • 相关阅读:
    VUE3 使用 Ant Design Vue 图标库的图标
    Vue3 前端获取数据后 “响应式表示” ref and reactive
    Vue3 使用 生命周期函数
    SpringBoot 前后端分离 跨域小问题
    Vue 使用 Ant Design Vue 。
    SB + Mybatis generator 实现模糊查询 且 过滤数据返回体 + Bean转换
    springboot+mybatis 利用插件生成代码
    spark-sql-03从mysql获取数据上传数据
    spark-sql-02
    sprak-sql-01-基础
  • 原文地址:https://www.cnblogs.com/farmer-y/p/5972709.html
Copyright © 2020-2023  润新知