March 2004
Intermediate to advanced
560 pages
26h 47m
English
using System;
using System.Globalization;
namespace Samples
{
public class MyFormatProviderSample: IFormatProvider,
ICustomFormatter
{
public object GetFormat(Type t)
{
if(t == typeof(ICustomFormatter))
return this;
return null;
}
public string Format(string f, object o,
IFormatProvider p)
{
if(f == null)
return String.Format("{0}", o);
if(!f.StartsWith("-"))
{
if(o is IFormattable)
{
return ((IFormattable)o).ToString(f, p);
}
else if(o != null)
{
return o.ToString();
}
}
return "Sample Formatted";
}
public static void Main()
{
MyFormatProviderSample m =
new MyFormatProviderSample();
Console.WriteLine(String.Format(
m,
"First (no formatting): {0} \n" +
"Second (formatted '-'): {1:-}",
new object[] {m, m}));
}
}
}
First ...
Read now
Unlock full access