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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:35:25+00:00 2026-06-02T19:35:25+00:00

Here, Person is a class, hence a reference type, it should be changed when

  • 0

Here, Person is a class, hence a reference type, it should be changed when I pass it as parameter in another function and assign it there.

Why program.person is null here after call to ChangePerson ?

namespace IndirectlyPropertySet
{
    class Program
    {
        public Person _person;

        public Person person
        {
            get { return _person; }
            set
            {
                if (_person != value)
                {
                    _person = value;
                }
            }
        }

        static void Main(string[] args)
        {
            Program program = new Program();

            ChangePerson(program.person); 
            // Why program.person is null after executing this ?

            program.person = new Person() { Name = "b", Age = 2 };
        }

        static void ChangePerson(Person p)
        {
            Person pe = new Person() { Name = "a", Age = 1 };
            p = pe;
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
  • 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-02T19:35:27+00:00Added an answer on June 2, 2026 at 7:35 pm

    As an addition to @JonSkeet’s answer, if you rewrote your code to do this instead it would work.

    static void ChangePerson(ref Person p)
    {
        Person pe = new Person() { Name = "a", Age = 1 };
        p = pe;
    }
    
    void Main()
    {
        Person p = program.person;
        ChangePerson(ref p);
        // program.person will now be set to "a" with age 1 at this point
        program.person = p;   
    
        program.person = new Person() { Name = "b", Age = 2 };
    }
    

    The key point is to pass the reference type by reference i.e. adding the ref keyword to your parameter.

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

Sidebar

Related Questions

For the sake of argument, here's a simple person class public class Person :
Here is some code: class Person def initialize(age) @age = age end def age
Here's part of my Django app's models.py : class Person(models.Model): birth_year = WideYear(null=True, blank=True)
Here is a Django model I'm using. class Person(models.Model): surname = models.CharField(max_length=255, null=True, blank=True)
I have this code here: var Person = (function() { var name; var PersonConstructor
In answering another persons question here on SO, I discovered that there is a
Here is model structure: Client is User, Client can be corporate or person: class
So here's my problem... Let's say I have a simple Person class just with
I have the classes: public class Person{ /* Props here */ } public class
Here's a sample of how it works now: [MetadataType(typeof(PersonMetadata))] public class Person { public

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.