CSharp examples for Custom Type:interface
Implement a Formattable Type
using System;/* w w w. j a v a 2 s .co m*/ public class Person : IFormattable { private string title; private string[] names; public Person(string title, params string[] names) { this.title = title; this.names = names; } public override string ToString() { return ToString("G", null); } public string ToString(string format, IFormatProvider formatProvider) { string result = null; if (format == null) format = "G"; switch (format.ToUpper()[0]) { case 'S': result = names[0][0] + ". " + names[names.Length - 1]; break; case 'P': if (title != null && title.Length != 0) { result = title + ". "; } for (int count = 0; count < names.Length; count++) { if (count != (names.Length - 1)) { result += names[count][0] + ". "; } else { result += names[count]; } } break; case 'I': // Use informal form - first name only. result = names[0]; break; case 'G': default: // Use general/default form - first name and surname. result = names[0] + " " + names[names.Length - 1]; break; } return result; } } class MainClass { public static void Main() { Person person = new Person("Mr", "Mary", "Glen", "Edith", "Xyz"); System.Console.WriteLine("Dear {0:G},", person); System.Console.WriteLine("Dear {0:P},", person); System.Console.WriteLine("Dear {0:I},", person); System.Console.WriteLine("Dear {0},", person); System.Console.WriteLine("Dear {0:S},", person); } }