IFormattable interface in C#

IFormattable interface define a method for formatting the value of an object into a string representation it contains the IFormattable.ToString(string, IFormatProvider) method.
  • 2769

The System.IFormattable interface define a method for formatting the value of an object into a string representation it contains the IFormattable.ToString(string, IFormatProvider) method.

Classes that require more control over the formatting of strings than Object.ToString provides should implement IFormattable, whose ToString method uses the current thread's CurrentCulture property.

Syntex

<ComVisibleAttribute(True)> _ Public Interface IFormattable

Example

using
 System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
 
class YourClass : IFormattableIComparable
{
    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();
    }
}

Thank You.....

© 2013 dotNetheaven. All rights reserved.