Custom Object Formatting : Custom Format : Development C# Examples


C# Examples » Development » Custom Format »

 

Custom Object Formatting









    
using  System;

class  Employee:  IFormattable
{
        int        id;
        string        firstName;
        string        lastName;

        public  Employee(int  id,  string  firstName,  string  lastName)
        {
                this.id  =  id;
                this.firstName  =  firstName;
                this.lastName  =  lastName;
        }
        public  string  ToString  (string  format,  IFormatProvider  fp)  
        {
                if  ((format  !=  null)  &&  (format.Equals("F")))
                        return(String.Format("{0}:  {1},  {2}",  id,  lastName,  firstName));
                else
                        return(id.ToString(format,  fp));
        }
}

class  MainClass
{
        public  static  void  Main()
        {
                Employee  fred  =  new  Employee(123,  "First",  "Last");
                Console.WriteLine("No  format:  {0}",  fred);
                Console.WriteLine("Full  format:  {0:F}",  fred);
        }
}
    
   
  
   



Output

No format: 123
Full format: 123: Last, First


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Development
» Custom Format