Overriding ToString() method

The ToString() method of C# is a virtually declared method. Therefore, it is possible to override the ToString() method in the inherited classes. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;


namespace ConsoleApplication6
{

    public class Money
    {
        private decimal amount;

        public decimal Amount
        {
            get
            {
                return amount;
            }

            set
            {
                amount = value;
            }
        }

        public override string ToString()
        {
            return "Rs." + amount.ToString();
        }

    }



    class Program
    {
        static void Main(string[] args)
        {

            Money m = new Money();
            m.Amount = 3000;
            Console.WriteLine(m.ToString());
          
        }        
        
        
    }
}

 

Add a Comment

Your email address will not be published. Required fields are marked *