in

Microsoft Philippines Community

A community for users, customers, and partners of Microsoft products in the Philippines :)

Trivia #3

Last post 01-19-2006 9:57 PM by Stanley Tan. 13 replies.
Page 1 of 1 (14 items)
Sort Posts: Previous Next
  • 01-18-2006 10:47 PM

    Idea [I] Trivia #3

    public class HeapPerson
    {
     public int ID;
     public string Name;
    }

    public struct StackPerson
    {
     public int ID;
     public string Name;
    }

    class Class1
    {
     [STAThread]
     static void Main(string[] args)
     {
      StackPerson person1 = new StackPerson();
      person1.ID = 1;
      person1.Name = "joey";
      ChangeDetail(person1);

      HeapPerson person2 = new HeapPerson();
      person2.ID = 2;
      person2.Name = "calisay";
      ChangeDetail(person2);
     }

     public static void ChangeDetail(StackPerson person)
     {
      person = new StackPerson();
      person.ID = 0;
      person.Name = "null";
     }

     public static void ChangeDetail(HeapPerson person)
     {
      person = new HeapPerson();
      person.ID = 0;
      person.Name = "null";
     }
    }

    We all know that for value types (including structs), a copy is passed to ChangeDetail method which is the reason why the contents of person1 did not change after the call to ChangeDetail.  How come for reference type (HeapPerson), the contents are also not changed after a call to ChangeDetail?

  • 01-18-2006 11:59 PM In reply to

    • cruizer
    • Top 10 Contributor
      Male
    • Joined on 07-20-2004
    • Singapore
    • Posts 9,417
    • Points 65,306
    • MVP

    Re: Trivia #3

    because of this statement:

    public static void ChangeDetail(HeapPerson person)
     {
      person = new HeapPerson();
      person.ID = 0;
      person.Name = "null";
     }

    the scope of the new HeapPerson class is local only to this ChangeDetail method, so it gets deallocated at the end of the execution of the method.
    http://devpinoy.org/blogs/cruizer

    Naglalayong buksan at palayain ang kamalayan ng Pinoy .NET developer
  • 01-19-2006 12:06 AM In reply to

    • dehranph
    • Top 25 Contributor
    • Joined on 11-14-2004
    • Ala eh taga Batangas!
    • Posts 1,769
    • Points 15,340

    Re: Trivia #3

    I guess I was wrong on my first answer and cruizer's right, commenting the highlighted text will change person2.

     cruizer wrote:
    because of this statement:
    public static void ChangeDetail(HeapPerson person)
     {
      person = new HeapPerson();
      person.ID = 0;
      person.Name = "null";
     }

    the scope of the new HeapPerson class is local only to this ChangeDetail method, so it gets deallocated at the end of the execution of the method.


    maybe, the rule of beforefieldinit does not apply here. :(
    Rodel E. Dagumampan
    Software Engineer - ASP.NET/C#
    Bel-air, Makati, Philippines

    Rodel E. Dagumampan
  • 01-19-2006 8:09 AM In reply to

    Re: Trivia #3

     cruizer wrote:
    because of this statement:

    public static void ChangeDetail(HeapPerson person)
     {
      person = new HeapPerson();
      person.ID = 0;
      person.Name = "null";
     }

    the scope of the new HeapPerson class is local only to this ChangeDetail method, so it gets deallocated at the end of the execution of the method.

    ditto...
    There's more than one way to do it.
    Hagonoy, Bulacan
  • 01-19-2006 8:48 AM In reply to

    • Sakuragi
    • Top 50 Contributor
      Male
    • Joined on 10-15-2003
    • Cavite
    • Posts 701
    • Points 6,286

    Re: Trivia #3

    I guess even if the person (HeapPerson) was not "newed-up" or re-initialized, the method will still change the values of its field. Big Smile [:D]
    Around here, however, we don't look backwards for very long. We keep moving forward, opening up new doors, and doing new things, because we're curious, and curiosity keeps leading us down new paths. - WD
  • 01-19-2006 9:59 AM In reply to

    Re: Trivia #3

     Sakuragi wrote:
    I guess even if the person (HeapPerson) was not "newed-up" or re-initialized, the method will still change the values of its field. Big Smile [:D]

    if it is newed, the method does not change the values....

  • 01-19-2006 10:11 AM In reply to

    Re: Trivia #3

     cruizer wrote:
    because of this statement:

    public static void ChangeDetail(HeapPerson person)
     {
      person = new HeapPerson();
      person.ID = 0;
      person.Name = "null";
     }

    the scope of the new HeapPerson class is local only to this ChangeDetail method, so it gets deallocated at the end of the execution of the method.

    then what if I pass the HeapPerson instance by reference, does its scope broaden beyond the ChangeDetail method? 

    "Scope" is often used to pertain to a particular variable (as I know and observe it).  And in this scenario, person1 variable's scope is within Main method where it is declared...

  • 01-19-2006 7:41 PM In reply to

    Re: Trivia #3

     Sakuragi wrote:
    I guess even if the person (HeapPerson) was not "newed-up" or re-initialized, the method will still change the values of its field. Big Smile [:D]

    You're right - since it's a reference.

    Stanley Tan
    Academic Program Manager
    Microsoft Singapore

    http://blogs.msdn.com/Stanley/
  • 01-19-2006 7:47 PM In reply to

    Re: Trivia #3

     cruizer wrote:
    because of this statement:

    public static void ChangeDetail(HeapPerson person)
     {
      person = new HeapPerson();
      person.ID = 0;
      person.Name = "null";
     }

    the scope of the new HeapPerson class is local only to this ChangeDetail method, so it gets deallocated at the end of the execution of the method.

    To expand on this, person is simply a pointer to person2.  By assigning a new value to person, you have removed that pointer.

    Stanley Tan
    Academic Program Manager
    Microsoft Singapore

    http://blogs.msdn.com/Stanley/
  • 01-19-2006 7:50 PM In reply to

    Re: Trivia #3

     joeycalisay wrote:

     cruizer wrote:
    because of this statement:

    public static void ChangeDetail(HeapPerson person)
     {
      person = new HeapPerson();
      person.ID = 0;
      person.Name = "null";
     }

    the scope of the new HeapPerson class is local only to this ChangeDetail method, so it gets deallocated at the end of the execution of the method.

    then what if I pass the HeapPerson instance by reference, does its scope broaden beyond the ChangeDetail method? 

    "Scope" is often used to pertain to a particular variable (as I know and observe it).  And in this scenario, person1 variable's scope is within Main method where it is declared...

    If you pass by reference, then... well, I guess I'll let the others take a stab here first.

    Stanley Tan
    Academic Program Manager
    Microsoft Singapore

    http://blogs.msdn.com/Stanley/
  • 01-19-2006 8:43 PM In reply to

    Re: Trivia #3

     Stanley Tan wrote:

     Sakuragi wrote:
    I guess even if the person (HeapPerson) was not "newed-up" or re-initialized, the method will still change the values of its field. Big Smile [:D]

    You're right - since it's a reference.

    but reading sakuragi's statement means be it re-newed/re-initialized or not, the method will still change the values which is not true with the original scenario...

  • 01-19-2006 9:16 PM In reply to

    Re: Trivia #3

     Stanley Tan wrote:

     cruizer wrote:
    because of this statement:

    public static void ChangeDetail(HeapPerson person)
     {
      person = new HeapPerson();
      person.ID = 0;
      person.Name = "null";
     }

    the scope of the new HeapPerson class is local only to this ChangeDetail method, so it gets deallocated at the end of the execution of the method.

    To expand on this, person is simply a pointer to person2.  By assigning a new value to person, you have removed that pointer.

    i think it is better/clearer to say that both person and person2 are references/pointer to a particular instance of HeapPerson in the heap...  what happened to these references/pointers and the heap instances is the point of this trivia question...

  • 01-19-2006 9:56 PM In reply to

    Re: Trivia #3

     joeycalisay wrote:
     Stanley Tan wrote:

     Sakuragi wrote:
    I guess even if the person (HeapPerson) was not "newed-up" or re-initialized, the method will still change the values of its field. Big Smile [:D]

    You're right - since it's a reference.

    but reading sakuragi's statement means be it re-newed/re-initialized or not, the method will still change the values which is not true with the original scenario...

    Yeah, I just assumed that was an English grammatical error and gave him the benefit of the doubt.

    Stanley Tan
    Academic Program Manager
    Microsoft Singapore

    http://blogs.msdn.com/Stanley/
  • 01-19-2006 9:57 PM In reply to

    Re: Trivia #3

     joeycalisay wrote:
     Stanley Tan wrote:

     cruizer wrote:
    because of this statement:

    public static void ChangeDetail(HeapPerson person)
     {
      person = new HeapPerson();
      person.ID = 0;
      person.Name = "null";
     }

    the scope of the new HeapPerson class is local only to this ChangeDetail method, so it gets deallocated at the end of the execution of the method.

    To expand on this, person is simply a pointer to person2.  By assigning a new value to person, you have removed that pointer.

    i think it is better/clearer to say that both person and person2 are references/pointer to a particular instance of HeapPerson in the heap...  what happened to these references/pointers and the heap instances is the point of this trivia question...

    Correct - person is simply a reference to the object referred to by person2.  By assigning it a new value, the reference changes.

    Stanley Tan
    Academic Program Manager
    Microsoft Singapore

    http://blogs.msdn.com/Stanley/
Page 1 of 1 (14 items)
Copyright © 2008 Microsoft Philippines Community

Powered by Community Server (Commercial Edition), by Telligent Systems