operator(+) overloading in c#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • enahar

    #1

    operator(+) overloading in c#

    Dear Sir/Madam,

    I have 2 lists as following:

    ApproverListBE appList1;

    ApproverListBE appList2 ;

    I want to add the above 2 lists.How do I override the + operator.



    Kind Regards,




  • Joshua Flanagan

    #2
    Re: operator(+) overloading in c#

    It depends on how you implemented ApproverListBE.
    For this example, I assume ApproverListBE derives from ArrayList:

    public class ApproverListBE : System.Collecti ons.ArrayList
    {
    public static ApproverListBE operator+(Appro verListBE list1,
    ApproverListBE list2)
    {
    ApproverListBE combinedList = new ApproverListBE( );
    combinedList.Ad dRange(list1);
    combinedList.Ad dRange(list2);
    return combinedList;
    }
    }

    You could then do something like:

    ApproverListBE x = new ApproverListBE( );
    ApproverListBE y = new ApproverListBE( );
    x.Add(1);
    x.Add(3);
    y.Add(10);
    y.Add(12);
    y.Add(14);
    ApproverListBE z = x + y;
    // z now contains 1,3,10,12,14


    Of course, you have to change the implementation of your + overload if
    you didn't derive from ArrayList (because you probably won't have the
    AddRange method). If you implement ICollection, you should be able to
    implement the functionality using the CopyTo() method.
    Hope that helps get you started.

    Joshua Flanagan



    enahar wrote:[color=blue]
    > Dear Sir/Madam,
    >
    > I have 2 lists as following:
    >
    > ApproverListBE appList1;
    >
    > ApproverListBE appList2 ;
    >
    > I want to add the above 2 lists.How do I override the + operator.
    >
    >
    >
    > Kind Regards,
    >
    >
    >
    >[/color]

    Comment

    Working...