public class Racer : IComparable<Racer>, IFormattable
{
public int Id { get; private set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Country { get; set; }
public int Wins { get; set; } public Racer(int id, string firstName, string lastName, string country = null, int wins = )
{
this.Id = id;
this.FirstName = firstName;
this.LastName = lastName;
this.Country = country;
this.Wins = wins;
} public override string ToString()
{
return String.Format("{0} {1}", FirstName, LastName);
} public string ToString(string format, IFormatProvider formatProvider)
{
if (format == null) format = "N";
switch (format.ToUpper())
{
case "N": // name
return ToString();
case "F": // first name
return FirstName;
case "L": // last name
return LastName;
case "W": // Wins
return String.Format("{0}, Wins: {1}", ToString(), Wins);
case "C": // Country
return String.Format("{0}, Country: {1}", ToString(), Country);
case "A": // All
return String.Format("{0}, {1} Wins: {2}", ToString(), Country, Wins);
default:
throw new FormatException(String.Format(formatProvider,
"Format {0} is not supported", format));
}
} public string ToString(string format)
{
return ToString(format, null);
} public int CompareTo(Racer other)
{
int compare = this.LastName.CompareTo(other.LastName);
if (compare == )
return this.FirstName.CompareTo(other.FirstName);
return compare;
}
}

C# IFormattable 接口重写的更多相关文章

  1. C# 自定义类型通过实现IFormattable接口,来输出指定的格式和语言文化的字符串(例:DateTime)

    常规的调用ToString()方法,存在两个问题. (1).调用者无法控制字符串的格式 (2).调用者不能方便的选择一种特定的语言文化来格式化字符串. 在开发一些国际化的应用时,应用程序需要调用与当前 ...

  2. 利用IFormattable接口自动参数化Sql语句

    提要 string.Format("{0},{1}",a,b)的用法大家都不陌生了,在很多项目中都会发现很多sql语句存在这样拼接的问题,这种做法很多"懒"程序 ...

  3. [C#]使用IFormattable接口来实现字符串格式化

    本文为原创文章.源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称.作者及网址,谢谢! 开发工具:VS2017 语言:C# DotNet版本:.Net FrameWork 4.0及以 ...

  4. 【C# IO 操作 】IFormatProvider接口|IFormattable 接口 格式化接口

    IFormatProvider接口获取一个满足要求的个格式化器. 方法 object? GetFormat(Type? formatType);GetFormat方法主要提供一个满足指定要求的对象,该 ...

  5. IFormattable,ICustomFormatter, IFormatProvider接口

    定                 义 1.IFormattable   提供一种功能,用以将对象的值格式化为字符串表示形式. 2.IFormatProvider  提供用于检索控制格式化的对象的机制 ...

  6. c# 接口代码实例

    类和接口的实现 接口定义:为一组方法签名指定一个名称的方式. 类实现接口,就一定要提供接口所有方法的实现. 即使抽象类,也要全部实现,但是,它可以把接口方法声明为abstract的,从而把这个接口方法 ...

  7. .NET基础 (13)IFormattable和IformatProvider的使用

    IFormattable和IformatProvider的使用1 如何使用IFormattable接口实现格式化输出2 如何告诉类型格式化输出的方式 IFormattable和IformatProvi ...

  8. luogg_java学习_07_抽象类_接口_多态学习总结

    这篇博客总结了半天,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 , 抽象 一种专门用来做父类,被继承的. (模板) 格式: abs ...

  9. Java基础学习笔记八 Java基础语法之接口和多态

    接口 接口概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”.接口只描述所应该具备的方法,并没有具体实现,具体的实现由接口的实现类(相当于接口的子类)来完成.这样将功能的定义 ...

随机推荐

  1. Nginx 开发者文档翻译 - 介绍

    由于我的一个Private Cloud FaaS(函数计算)项目 Navagraha 使用nginx作为实现Service Mesh和API Gateway的基础框架,需要对Nginx有深入的了解,因 ...

  2. [转帖]新一代IBM Z14主机技术介绍

    新一代IBM Z14主机技术介绍 https://cloud.tencent.com/developer/news/268909 IBM最新的已经有IBM Z15 主机了.. 文章来源:企鹅号 - 云 ...

  3. 数据结构 -- Trie字典树

    简介 字典树:又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种. 优点:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高. 性质:   1.  根节 ...

  4. Python--yaml文件操作

    一.yaml文件的使用注意事项 加载yaml文件函数: data = yaml.load(f)  (我在实际测试中用的是yaml.safe_load(f)可以正常,用yaml.load(f)会报错因为 ...

  5. //统计报表-供水量统计主列表分页查询 Element-ui的分页插件

    <!-- //分页 --> <div class="pagination">时间(月) <el-pagination @current-change= ...

  6. Git拉取代码切换分支

    1.克隆代码 cd d:/GitTest //指定存放的目录 git clone https://git.oschina.net/name/test.git //你的仓库地址 2.查看远程所有分支 g ...

  7. Task执行多次

    项目中,曾经出现过启动时数据库连接数瞬间增大,当时并没有注意该问题. 后期,由于Task任务多次执行,才着手查看这个问题,经排查,由于tomcat中webapp配置多次,导致webapp被扫描多次(配 ...

  8. winform c# 请求网站,返回Json字符串

    private void callApibjhb() { //输出执行的开始时间 Console.WriteLine(string.Format("Bind {0}", DateT ...

  9. Lua table直接索引VS缓存索引性能测试小示例

    local p = {} p.t = {} p.t.p = {} p.t.p.t = {} p.t.p.t.p = {} p.t.p.t.p.t = {} p.t.p.t.p.t.p = {} p.t ...

  10. JavaScript--关于闭包(closure)

    js代码在执行前会做的几件事情: 1.代码检测 2.预编译:在执行代码之前会对代码中的函数以及变量提前声明 并且做一些其他的处理 1.函数在执行前的一瞬间,会生成一个OA(object action) ...