ICustomFormatter IFormatProvider : ICustomFormatter « Class « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Class » ICustomFormatter 
7.52.1.ICustomFormatter IFormatProvider
/*
Quote from
Accelerated C# 2005
# By Trey Nash
# ISBN: 1-59059-717-6
# 432 pp.
# Published: Aug 2006
*/

using System;
using System.Text;
using System.Globalization;

public class ComplexDbgFormatter : ICustomFormatter, IFormatProvider
{
    public object GetFormatType formatType ) {
        ifformatType == typeof(ICustomFormatter) ) {
            return this;
        else {
            return CultureInfo.CurrentCulture.
                GetFormatformatType );
        }
    }

    public string Formatstring format, object arg, IFormatProvider formatProvider ) {
        ifarg.GetType() == typeof(Complex&& format == "DBG" ) {
            Complex cpx = (Complexarg;

            StringBuilder sb = new StringBuilder();
            sb.Appendarg.GetType().ToString() " " );
            sb.AppendFormat"Real:{0} ", cpx.Real );
            sb.AppendFormat"Img: {0} ", cpx.Img );
            return sb.ToString();
        else {
            IFormattable formatable = arg as IFormattable;
            ifformatable != null ) {
                return formatable.ToStringformat, formatProvider );
            else {
                return arg.ToString();
            }
        }
    }
}

public struct Complex : IFormattable
{
    public Complexdouble Real, double Img ) {
        this.Real = Real;
        this.Img = Img;
    }

    public string ToStringstring format, IFormatProvider formatProvider ) {
        StringBuilder sb = new StringBuilder();
        sb.Append"( " + Real.ToString(format, formatProvider) );
        sb.Append" : " + Img.ToString(format, formatProvider) );
        sb.Append" )" );

        return sb.ToString();
    }

    public double Real;
    public double Img;
}

public class MainClass
{
    static void Main() {
        CultureInfo local = CultureInfo.CurrentCulture;
        CultureInfo germany = new CultureInfo"de-DE" );

        Complex cpx = new Complex12.34561234.56 );

        string strCpx = cpx.ToString"F", local );
        Console.WriteLinestrCpx );

        strCpx = cpx.ToString"F", germany );
        Console.WriteLinestrCpx );

        ComplexDbgFormatter dbgFormatter = new ComplexDbgFormatter();
        strCpx = String.FormatdbgFormatter,"{0:DBG}",  cpx );
        Console.WriteLine"\nDebugging output:\n{0}", strCpx );
    }
}
( 12.35 : 1234.56 )
( 12,35 : 1234,56 )

Debugging output:
Complex Real:12.3456 Img: 1234.56
7.52.ICustomFormatter
7.52.1.ICustomFormatter IFormatProvider
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.