C# Aggregate for a class properties

trying to comprehend that the 'output'.length will always greater than zero after it is aggregated. For example,

1. output.length is zero at first
2. output.length is 5 after having 'Gates'.
3. output.length is 14 after having 'Gates, Ballmer'.
4. output.length is 20 after having 'Gates, Ballmer, Jobs'.
5. output.length is (always more than 0),
namespace aggregate
{
    class Program
    {
        static void Main(string[] args)
        {
            Person[] people = new Person[]
            {
                new Person() { FirstName = "Bill", LastName="Gates"},
                new Person() { FirstName = "Steve", LastName="Ballmer"},
                new Person() { FirstName = "Steve", LastName="Jobs"}
            };

            Console.WriteLine(people.Aggregate("",
            (output, next) =>
            output.Length > 0 ? output + ", " + next.LastName
            : next.LastName));

            Console.ReadKey();

        }

        

        class Person
        {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        };
    }
}

Open in new window

LVL 1
doramail05Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
trying to comprehend that the 'output'.length will always greater than zero after it is aggregated

It should, provided your list isn't empty and all of your LastName's aren't empty.
käµfm³d 👽Commented:
and all of your LastName's aren't empty.

I could probably phrase that better as, "it is not the case that every LastName in the list is empty string."
Fernando SotoRetiredCommented:
Can you please restate your question I am not understanding what you are asking.

Thanks
Fernando
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Fernando SotoRetiredCommented:
Hi doramail05;

This statement, "5. output.length is (always more than 0), ", is not true. The very first time through it is zero as you can see from the seed of the Aggregate method which is set to "", empty string.

Fernando
käµfm³d 👽Commented:
@FernandoSoto

The very first time through it is zero...

While I agree, if I am interpreting the intent of the code correctly, then you would want the initializer to be an empty string in order to print the list properly.
Fernando SotoRetiredCommented:
@kaufmed

But it is set to an empty string. Looking at the Aggregate method below the very first parameter is an empty string, "", the very first time the method is executed the first parameter called the seed is assigned to output variable and for every time after it is assigned by the delegate function in the Aggregate.

people.Aggregate([b]""[/b], (output, next) =>
            output.Length > 0 
            ? output + ", " + next.LastName
            : next.LastName)

Open in new window


Fernando
Fernando SotoRetiredCommented:
@kaufmed

This code editor does not work well all the time the code should look like this.

people.Aggregate("", (output, next) =>
            output.Length > 0
            ? output + ", " + next.LastName
            : next.LastName)
doramail05Author Commented:
while the seed is set to "", the first output.length will be zero.

when it comes to the situation where it does not found any properties (after "Jobs"), it will set back to zero/empty again?
käµfm³d 👽Commented:
@FernandoSoto

Looking at the Aggregate method below the very first parameter is an empty string, "",

I'm not disagreeing with you, I'm simply saying that I think that would be an intended consequence for this particular situation. Setting it to empty string allow the list to print as:

    Gates, Ballmer, Jobs

Setting to something else would print the list as (with "something else" being "Hello World!", for example):

    Hello World!, Gates, Ballmer, Jobs
käµfm³d 👽Commented:
when it comes to the situation where it does not found any properties (after "Jobs"), it will set back to zero/empty again?

Once the Aggregate() method completes, "output" is out of scope, so its value is irrelevant. Calling the method a second time would re-initialize (and recreate) the variable again.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.