简体   繁体   中英

Calling method with dot operator on list of objects

I am relatively new to C# and in my assignment I am asked to create a list of object:

var A = new List<ObjectName>;

And do operation on this list by doing such function calls

int sum = A.Add(B); //B is also a List<ObjectName>

How do I implement such method in "ObjectName" class, I know you can do following with primitive datatypes:

   public int Add(ObjectName B){
        return this.Value + B.Value
}

How do I do the same when dealing with the lists? (In case I dont sound clear, I want to get the whole list which is calling the function Add() as well as whole list B which is passed as argument)

EDIT: Add() was just an example (as it was easy to demonstrate for primitive datatypes). I am not adding anything, I just want to access both caller list as well as passed list in same function, which is called by dot operator.

How do I implement such method in "ObjectName" class

You cannot. The method works on a List<> and nothing you do in your class will change that. I strongly suggest you ask your teacher to clarify this.

You can make your syntax compile by adding an extension method:

public static class ListOfObjectNameExtension
{
    public static int Add(this List<ObjectName> a, List<ObjectName> b)
    {
        // no idea what the result should actually be...
        return 42;
    }
}

The following:

int sum = A.Add(B);

will now compile, because we extended the List<ObjectName> to have an .Add method.

But again, I'm sure this is not what your teacher wanted, please go ask them to clarify the task.

To make a sum of object values use System.Linq namespace. Put 'System.Linq' in using block. (Top of your program). This is a quite useful library. Here info link LINQ

'Union' makes one general collection removing duplications and Sum - summarize your values of objects. 'Concat' makes one simple general collection including all your elements.

    public int MakeSumWithoutDuplications(List<ObjectName> a, List<ObjectName> b)
    {
        return a.Union(b).Sum(x => x.Value);
    }

    public int MakeGeneralSum(List<ObjectName> a, List<ObjectName> b)
    {
        return a.Concat(b).Sum(x => x.Value);
    }

It's not entirely clear what you mean, but I'm -guessing- you want to add the sum of the values of .Value in every item in list A to the sum of the .Value properties in list B.

To do this, you can write an extension method for List:

public static class ListExtensions
{
    public static int SomeAddOperation(this List<ObjectName> listA, List<ObjectName> listB)
    {
        int sumA = 0;
        foreach(var someObject in listA)
        {
            sumA += someObject.Value;
        }

        int sumB = 0;
        foreach(var someObject in listB)
        {
            sumB += someObject.Value;
        }        

        return sumA + sumB;
    }
}

Then, you call it like this:

int sum = A.SomeAddOperation(B);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM