Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8297979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:36:11+00:00 2026-06-08T15:36:11+00:00

The .Last() method for Lists only returns a value. I would like to be

  • 0

The .Last() method for Lists only returns a value. I would like to be able to do something like this.

  List<int> a = new List<int> { 1, 2, 3 };
  a.Last() = 4;

This is my attempt at writing an extension method (it does not compile)

public unsafe static T* mylast<T>(this List<T> a)
{
   return &a[a.Count - 1];
}

Is what I want to do possible?

edit:

This is an example of where I would want to use it.

 shapes.last.links.last.points.last = cursor;   //what I want the code to look like
 //how I had to write it.
 shapes[shapes.Count - 1].links[shapes[shapes.Count - 1].links.Count - 1].points[shapes[shapes.Count - 1].links[shapes[shapes.Count - 1].links.Count - 1].points.Count-1] = cursor;

This is why doing

shapes[shapes.Count-1] 

isn’t really a solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:36:14+00:00Added an answer on June 8, 2026 at 3:36 pm

    I’d recommend Thom Smith’s solution, but if you’d really like to have property-like access, why not just use a property?

    public class MyList<T> : List<T>
    {
        public T Last
        {
            get
            {
                return this[this.Count - 1];
            }
            set
            {
                this[this.Count - 1] = value;
            }
        }
    }
    

    Used like:

    var m = new MyList<int> { 1, 2, 3 };
    m.Last = 4;
    Console.WriteLine(m.Last);
    

    No unsafe code, so it’s better. Also, it doesn’t preclude the use of LINQ’s Last method (the compiler can tell the two apart due to how they’re used).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to implement method that takes arbitrary Seq[T] and returns Seq[T] as
I would like to have the last query (for debugging purposes) from the PDOStatement.
For the last few days I have been attempting to find a method to
In Iterator Sun added the remove method to remove the last accessed element of
There is a method Regex.Replace(string source, string pattern, string replacement) The last parameter supports
In my program, I am creating several threads in the main() method. The last
(No route matches [DELETE] /subscriptions) using <%= link_to 'Unsubscribe', subscriptions_path(feed_id: session[:read_random].last), method: :delete, remote:
Last night I tried to put together something that I have had working since
I have a JavaFX app with a some code like this... public class MainListener
I have a generic methode like this one: public class ClassName { public <T>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.