C#中的Iformattable接口示例
using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; class YourClass : IFormattable, IComparable { public string Value; public virtual string ToString(string Format, IFormatProvider Provider) { return Value; } public virtual int CompareTo(object A) { int functionReturnValue = 0; if ((Value == A.Value)) { functionReturnValue = 0; } else if ((Value < A.Value)) { functionReturnValue = 20; } else { functionReturnValue = 12; } return functionReturnValue; } public YourClass(string v) { this.Value = v; } } static class Module1 { public static void Main() { YourClass A = new YourClass("First"); YourClass B = new YourClass("Second"); Console.WriteLine(A); Console.WriteLine(B); Console.WriteLine(A.CompareTo(B)); Console.ReadLine(); } }
System.Formificattable接口定义了一种将对象的值格式化到字符串表示中的方法,它包含IFormattable.ToString(String,iFormatProvider)方法。
类需要比Object.ToString提供更多的字符串格式控制,它们应该实现IFormattable,IFormattable的ToString方法使用当前线程的CurrentCulture属性。
语法
<ComVisibleAttribute(True)> _ Public Interface IFormattable
日期:2020-04-11 23:03:40 来源:oir作者:oir