6/01/2010

C# Method Returning an Object



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

namespace MethodReturningAnObject
{
    class points
    {
        int x, y;
        public points()//default constructor
        {
            x = y = 0;
        }
        public points(int x,int y)//Parameterized constructor
        {
            this.x = x;
            this.y = y;
        }
        public void print()
        {
            Console.WriteLine(x + " " + y);
        }
        //this method is returning an object
        public points add(points p1,points p2)
        {
            points p3 = new points();
            p3.x = p1.x + p2.x;
            p3.y = p1.y + p2.y;
            return p3;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            points p1 = new points(4,7);
            points p2 = new points(1, 2);

            points p3 = new points();
            p1.print();
            p2.print();
            p3 = p3.add(p1, p2);
            Console.WriteLine("-----------");
            p3.print();


        }
    }
}

No comments:

Post a Comment