登山-C#-IFormattable之自定义string的现实格式

C#中string是比较特别的。值得注意。

在讨论到string时自然少不了提及到格式化现实的话题,以前都是看中各个书中讲解如何将string按特定的格式输出,今日来研究一下自定义string的格式进行输出。

此处借《C#高级编程》中的一段代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LINQLearn
{
    [Serializable]
    class Racer : IComparable<Racer>,IFormattable
    {
        public Racer(string firstName = null, string lastName = null,
            string country = null, int start = 0, int wins = 0,
            IEnumerable<int> years = null, IEnumerable<string> cars = null)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Country = country;
            this.Starts = Starts;
            this.Wins = wins;
            var yearsList = new List<int>();
            foreach (var year in years)
            {
                yearsList.Add(year);
            }
            this.Years = yearsList.ToArray();
            var carList = new List<string>();
            foreach (var car in cars)
            {
                carList.Add(car);
            }
            this.Cars = carList.ToArray();
        }

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Wins { get; set; }
        public string Country { get; set; }
        public int Starts { get; set; }
        public string[] Cars { get; private set; }
        public int[] Years { get; private set; }
        public override string ToString()
        {
            return String.Format("{0},{1}",FirstName,LastName);
        }

        public int CompareTo(Racer other)
        {
            if (other == null) throw new ArgumentNullException("ohter");
            return this.LastName.CompareTo(other.LastName);
        }

        public  string ToString(string format)
        {
            return ToString(format, null);
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            switch (format)
            {
                case null:
                case "N":
                    return ToString();
                case "F":
                    return FirstName;
                case "L":
                    return LastName;
                case "C":
                    return Country;
                case "S":
                    return Starts.ToString();
                case "W":
                    return Wins.ToString();
                case "A":
                    return String.Format("{0}, {1}, {2}; starts:{3}, wins:{4}", FirstName, LastName, Country, Starts, Wins);
                default:
                    throw new FormatException(String.Format("Format {0} not supported",format));
            }
        }
    }
}

IFormattable接口只定义了一个方法,该方法也命名为ToString(),他带有两个参数,

interface IFormattable
{
   string ToString(string format,IFormatProvider formatProvider);
}
这个ToString方法的第一个参数是一个字符串,他指定要求的格式。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值