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

  • SEARCH
  • Home
  • 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 7883927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:39:49+00:00 2026-06-03T04:39:49+00:00

1.First I defined an extension method for the IEnumerable.Add() like the code below public

  • 0

1.First I defined an extension method for the IEnumerable.Add() like the code below

 public static IEnumerable<T> Add<T, TKey>(this IEnumerable<T> enumerable, T value, Func<T, TKey> orderBy)
    {
        if (enumerable == null)
            return null;

        if (enumerable is IList<T>)
        {
            var list = enumerable as IList<T>;
            if (!enumerable.Contains(value))
            {
                list.Add(value);
                enumerable = enumerable.OrderBy(orderBy);
            }
        }
    }

2.Then,I raised the extension method like this to sort the itemlist according to the “Date” property when a new item was added to the list:

   itemList.Add(item, o => o.Date);

3.After all,it appears that the “itemList” was not sorted.

4.I followed the extension method and found that “enumerable” was a new instance after “enumerable = enumerable.OrderBy(orderBy)” and it was sorted,but the “list” was not.

5.Then I tried to cast the sorted enumerable to list like “list=enumerable.ToList()”,both of them(“enumerable” and “list”) were sorted.

6.After that ,when the call stack went back to the “itemList.Add(item, o => o.Date);”,the “itemList” was not sorted at all!!!

Anyone can give me some advices?Thanks a looooooooooooooooooooooooooooooooot!!

  • 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-03T04:39:51+00:00Added an answer on June 3, 2026 at 4:39 am

    I believe your problem is that the reference to enumerable is being passed by value rather than by reference. See Jon Skeet’s article about passing parameters by value or reference for more information about what that means. In short, C# passes a copy of the parameter’s reference so assigning a new value to parameter does not change the reference of the object that was passed in. To pass a parameter by reference you specify the ref keyword, but I don’t think that will work with an extension method. If you’re dead set on making this work I would suggest inserting the items into your List in sorted order, probably requiring that T implement IComparable.

    Update:

    First off, see the Skeet’s article it’s really quite informative and I will probably only be half as clear as he is. Second, when you pass an object as a parameter to a method you are passing a copy of the reference. This means you can still access members of the object but, the same way that a value type is passed by copy, if you modify the reference (ie assign it a new value) you wont modify the original reference. Specifying ref means that you are passing a reference to the reference and changing the reference (assigning a value to it) will affect the original object.

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

Sidebar

Related Questions

What is the meaning of the .NET 3.5 extension method Enumerable.First() when you call
I have an entity defined as in a db First Model: public class Merlin_BR_Condiciones_Item
We are using EF 4.0 with code first approach. I have defined constraint for
Title says it mostly. I want to add a simple extension method to the
This is an extension question to: how-do-i-create-a-self-signed-certificate-for-code-signing-on-windows To take this further, if I have
According to this post , F# supports extension methods on object instances and static
I'm having a problem with scala generics. While the first function I defined here
First: where are std::move and std::forward defined? I know what they do, but I
I have a TabActivity with three tabs defined. The first tab is light-weight and
I tried to run this code: #define ROW_CNT 8; #define COLUMN_CNT 24; #define FIRST_COLUMN

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.