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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:52:29+00:00 2026-05-26T15:52:29+00:00

In C++ I could do something like this class Person { House * myhouse;

  • 0

In C++ I could do something like this

class Person
{
    House * myhouse;
}

class House
{
    std::vector<Person*> members;
}

How can I do a similar thing in C#?

  • 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-26T15:52:29+00:00Added an answer on May 26, 2026 at 3:52 pm
    public class Person
    {
        public House MyHouse { get; set; }
    }
    
    public class House
    {
        public List<Person> Members { get; private set; }
    
        public House()
        {
            this.Members = new List<Person>();
        }
    }
    

    Instead of fields here i’m using properties, in particular, automatic properties.
    This is both for being cleaner, exposing a field to the outer world is usually less clean than exposing a property, and also because i can control in this way how people can access to the properties for read and for write. In this example, property Members is public for read but private for write, I initialize it in the constructor.

    In C# there is not the concept of objects allocated in stack, objects are always allocated in heap.

    This means classes are always reference types and a variable of type List is a reference to an object of type List, like a pointer is in C++.
    For this reason you need to use the new operator to allocate it, or the default value will be null.

    Of course, as you know, in C# there is the garbage collector, so you don’t need to delete the object.

    In C# there are also value types, basic types like int, float, double and struct are value types, and they works in a different way indeed.

    Arrays and strings are still reference types (classes).

    Also note that in C# class fields are initialized by default in the constructor to 0, each type you can think of will be initialized with 0, so, a pointer will be null, a float will be 0.0f, a struct will be a struct with all fields set to 0. Like a calloc in C.

    There is however another totally different approach possible.
    We can use the base class Collection and make the MyHouse property totally transparent and safe: we set it when we change the collection, this technique is used often.

        public class Person
        {
            // This field is internal, it means that all classes in the same module (in the same dll for example) can access to this field.
            // This keyword was introduced for the same reason that the "friend" keyword exists in C++.
            // We need this internal so we can modify it from House class.
            internal House house;
    
            public House MyHouse
            {
                get { return this.house; }
            }
        }
    
        public class House :
            System.Collections.ObjectModel.Collection<Person>
        {
            // We shadow the base member, this is faster than default implementation, O(1).
            public new bool Contains(Person person)
            {
                return person != null && person.house == this;
            }
    
            protected override void RemoveItem(int index)
            {
                Person person = this[index];
                base.RemoveItem(index);
                person.house = null;
            }
    
            protected override void SetItem(int index, Person item)
            {
                if (item == null)
                    throw new ArgumentNullException("Person is null");
                if (item.house != null)
                    throw new InvalidOperationException("Person already owned by another house");
                Person old = this[index];
                base.SetItem(index, item);
                old.house = null;
                item.house = this;
            }
    
            protected override void InsertItem(int index, Person item)
            {
                if (item == null)
                    throw new ArgumentNullException("Person is null");
                if (item.house != null)
                    throw new InvalidOperationException("Person already owned by another house");
                base.InsertItem(index, item);
                item.house = this;
            }
    
            protected override void ClearItems()
            {
                foreach (Person person in this)
                {
                    person.house = null;
                }
                base.ClearItems();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Django model that looks something like this: class Person(models.Model): name =
I'm developing a REST service, so a request could be something like this: /Data/Main/Table=Customers/
I'd like to be able to do this so that I could do something
I've trying to achieve something like this: class Base { public: Base(string S) {
In the Modern Objective-C runtime, you can do something like this: @interface MyClass :
Lets say I have a set of model classes like this: public class Person
I generally want to do something like this: CriteriaBuilder qb = em.getCriteriaBuilder(); CriteriaQuery<Person> c
I develop in C++, and sometimes I wish I could say something like this:
I'm wishing I could do something like the following in SQl Server 2005 (which
For example, The function could be something like def RandABCD(n, .25, .34, .25, .25):

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.