C#: Designing a class

This entry is part 21 of 71 in the series C#

Say we have a friend who has developed a class called Swap.

From the brief documentation given by our friend, we know that there is a method that will swap 2 strings and our friend has also provided us a method to display the strings.

The example below will demonstrate how we can make use of the Swap class to swap 2 strings.

using System;

namespace SwapSpace
{
    class Swap
    {
        string lhs;
        string rhs;

        public Swap(string _lhs, string _rhs)
        {
            lhs = _lhs;
            rhs = _rhs;
        }

        public void swap(string _lhs, string _rhs)
        {
            string temp;
            temp = _lhs;
            lhs = rhs;
            rhs = temp;
        }

        public void display()
        {
            Console.WriteLine(lhs + " " + rhs);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string l = "left";
            string r = "right";

            Swap swapStr = new Swap(l,r);
            swapStr.swap(l, r);
            swapStr.display();

            Console.ReadKey();
        }
    }
}

swap

 

 

Series Navigation<< C#: Assigning an instance
C#: Static members >>