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 84935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:00:36+00:00 2026-05-10T22:00:36+00:00

Why are the lists list1Instance and p in the Main method of the below

  • 0

Why are the lists list1Instance and p in the Main method of the below code pointing to the same collection?

class Person     {         public string FirstName = string.Empty;         public string LastName = string.Empty;          public Person(string firstName, string lastName) {             this.FirstName = firstName;             this.LastName = lastName;         }     }      class List1     {         public List<Person> l1 = new List<Person>();          public List1()         {             l1.Add(new Person('f1','l1'));             l1.Add(new Person('f2', 'l2'));             l1.Add(new Person('f3', 'l3'));             l1.Add(new Person('f4', 'l4'));             l1.Add(new Person('f5', 'l5'));         }         public IEnumerable<Person> Get()         {             foreach (Person p in l1)             {                 yield return p;             }              //return l1.AsReadOnly();          }      }        class Program     {          static void Main(string[] args)         {             List1 list1Instance = new List1();              List<Person> p = new List<Person>(list1Instance.Get());                         UpdatePersons(p);              bool sameFirstName = (list1Instance.l1[0].FirstName == p[0].FirstName);         }          private static void UpdatePersons(List<Person> list)         {             list[0].FirstName = 'uf1';         }     } 

Can we change this behavior with out changing the return type of List1.Get()?

Thanks

  • 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. 2026-05-10T22:00:36+00:00Added an answer on May 10, 2026 at 10:00 pm

    In fact, IEnumerable<T> is already readonly. It means you cannot replace any items in the underlying collection with different items. That is, you cannot alter the references to the Person objects that are held in the collection. The type Person is not read only, however, and since it’s a reference type (i.e. a class), you can alter its members through the reference.

    There are two solutions:

    • Use a struct as the return type (that makes a copy of the value each time it’s returned, so the original value will not be altered — which can be costly, by the way)
    • Use read only properties on the Person type to accomplish this task.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lists in C# have the .ToArray() method. I want the inverse, where an array
in Lists - I can always check that b=a points to same object and
I have several lists which may or may not be empty. I want to
I have two lists of tuples which are as follows: [(String,Integer)] and [(Float,Integer)] .
I have two lists old and new , with the same number of elements.
I have 3 generic lists: List<string> input List<string> compareTo List<string> results I'd like to
Dictionaries and lists defined directly under the class definition act as static (e.g. this
I have two lists of the same type. That type does not have an
I have multiple lists like below [u'a', 11, u'P'] [u'a', 11, u'A'] [u'b', 2,
For lists, the method list.index(x) returns the index in the list of the first

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.