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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:46:27+00:00 2026-05-24T17:46:27+00:00

This demo class to explain the question public class SomeClass { public string Name

  • 0

This demo class to explain the question

 public class SomeClass
    {
        public string Name { get; set; }
        public int Age { get; set; }

    }

While developing something equals to the sample code I included in this question I got the following thought:

Since classes are reference type, and if I assign multiple instances to the same class object using loop and store these objects in an list, isn’t that enough to ruin each object and make it equals to last instance assigned to it?

Here’s some sample implementation for the confusion

List<SomeClass> lst = new List<SomeClass>();
SomeClass someClassObj = null;

for (int i = 0; i < 3; i++)
{
     someClassObj = new SomeClass();
     someClassObj.Name = "Name " + i.ToString();
     someClassObj.Age = i;
     lst.Add(someClassObj);
}

after testing it does not wokred the way I though it would, anyway that what i want
anyone help to clear this confusion.

  • 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-05-24T17:46:28+00:00Added an answer on May 24, 2026 at 5:46 pm

    Every time you do

    someClassObj = new SomeClass();

    a new memory piece is created in heap and it’s address is assigned to someClassObj which means in your list there is not actually only single address which you are adding again and again but a new address and that’s why when you will compare the object they won’t be same because they have a different address

    initially

    SomeClass someClassObj = null; your object is pointing to nothing. when you create a new instance using new () your object starts pointing to newly allocated memory’s address. So, inside loop, every instance is assigned new address and that address is stored in SomeClass which is same pointer/reference. When you add item in the list, it actually adds the address/reference of item that is currently pointed by SomeClass

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

Sidebar

Related Questions

Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
if i use this : class A(db.Model): a=db.StringProperty() class demo(BaseRequestHandler): def get(self): a=A() a.a='sss'
So I have this demo project almost completely working. public class Project { public
1st code: #include <iostream> using namespace std; class demo { int a; public: demo():a(9){}
Please have a look at this demo page that I cooked up a while
This is a 2 part question: 1) click on one of the demo dropdowns
Let's start with this demo #include <iostream> using namespace std; template <class T> void
unfortunately this question may get closed but i hope some people answer it. I
http://jsfiddle.net/aBaw6/2/ This demo does not add class when you hover a list item. What
see this demo from jquery ui you have to hold down the Ctrl key

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.