lists, get last item, 2 second question

  • Thread starter Thread starter Nick Valeontis
  • Start date Start date
N

Nick Valeontis

Is there a way to get the last item added to a generic list? That is,
without using the count property.


Something to improve this code:


schedule.jobs[schedule.jobs.Count-1].tasks.Add( ... );



With regards,

Nick
 
Nothing wrong.

I can't remember where (book or site etc) but I would swear I have seen a
better way to get the last index of a generic list.

Like a property, eg : schedule.jobs.getlastitem.tasks.add( ...);

But I can't find it now, so I guess it was in my imagination, or perhaps a
different language ;-)


With regards,

Nick


Daniel said:
What's wrong with using that code?



Nick Valeontis said:
Is there a way to get the last item added to a generic list? That is,
without using the count property.


Something to improve this code:


schedule.jobs[schedule.jobs.Count-1].tasks.Add( ... );



With regards,

Nick
 
Nick,

I suppose you could do the following.

schedule.jobs.FindLast(delegate(Job x) { return true; });

That's assuming that FindLast actually starts it search from the last
item. It's pretty ugly if you ask me.

A lambda expression would be pretty elegant; perhaps more so than the
..Count - 1 technique.

schedule.jobs.FindLast(x => true);

You'll have to wait until C# 3.0 for that though.

Brian


Nick said:
Nothing wrong.

I can't remember where (book or site etc) but I would swear I have seen a
better way to get the last index of a generic list.

Like a property, eg : schedule.jobs.getlastitem.tasks.add( ...);

But I can't find it now, so I guess it was in my imagination, or perhaps a
different language ;-)


With regards,

Nick


Daniel said:
What's wrong with using that code?



Nick Valeontis said:
Is there a way to get the last item added to a generic list? That is,
without using the count property.


Something to improve this code:


schedule.jobs[schedule.jobs.Count-1].tasks.Add( ... );



With regards,

Nick
 
K, thanks Brian!

I will have that in mind.


Best regards,

Nick

Brian Gideon said:
Nick,

I suppose you could do the following.

schedule.jobs.FindLast(delegate(Job x) { return true; });

That's assuming that FindLast actually starts it search from the last
item. It's pretty ugly if you ask me.

A lambda expression would be pretty elegant; perhaps more so than the
.Count - 1 technique.

schedule.jobs.FindLast(x => true);

You'll have to wait until C# 3.0 for that though.

Brian


Nick said:
Nothing wrong.

I can't remember where (book or site etc) but I would swear I have seen a
better way to get the last index of a generic list.

Like a property, eg : schedule.jobs.getlastitem.tasks.add( ...);

But I can't find it now, so I guess it was in my imagination, or perhaps
a
different language ;-)


With regards,

Nick


Daniel said:
What's wrong with using that code?



Is there a way to get the last item added to a generic list? That is,
without using the count property.


Something to improve this code:


schedule.jobs[schedule.jobs.Count-1].tasks.Add( ... );



With regards,

Nick
 
You may be thinking of a linked list that has Last and RemoveLast methods.
Depending on what your doing a LL may be a better choice anyway.

--
William Stacey [C# MVP]

| Nothing wrong.
|
| I can't remember where (book or site etc) but I would swear I have seen a
| better way to get the last index of a generic list.
|
| Like a property, eg : schedule.jobs.getlastitem.tasks.add( ...);
|
| But I can't find it now, so I guess it was in my imagination, or perhaps a
| different language ;-)
|
|
| With regards,
|
| Nick
|
|
| | > What's wrong with using that code?
| >
| >
| >
| > | >> Is there a way to get the last item added to a generic list? That is,
| >> without using the count property.
| >>
| >>
| >> Something to improve this code:
| >>
| >>
| >> schedule.jobs[schedule.jobs.Count-1].tasks.Add( ... );
| >>
| >>
| >>
| >> With regards,
| >>
| >> Nick
| >>
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top