简体   繁体   English

C#为什么创建对单个对象的多个引用?

[英]C# Why create multiple references to a single object?

Situation: I recently came across an article where an employer was asking entry-level candidates to solve a complex reference problem (not shown), as well as questions pertaining to the stack and managed heap. 情况:最近我碰到一篇文章,其中一位雇主要求入门级候选人解决​​一个复杂的参考问题(未显示),以及与堆栈和托管堆有关的问题。 I have devoted a bit of time to learning these concepts for personal understanding, but was not aware that these are considered 'entry-level' knowledge. 我花了一些时间来学习这些概念以进行个人理解,但是并没有意识到它们被认为是“入门级”知识。 Being a CIS major apposed to CS (huge difference I know) these concepts are not taught. 作为适合CS的CIS专业(我知道有很大的不同),这些概念并未教授。

Question Why would a developer assign an object reference to another reference? 问题为什么开发人员将对象引用分配给另一个引用? More succinctly, why would a developer ever flip flop dozens of references around as described in interview scenarios? 更简洁地说,为什么开发人员会像面试场景中描述的那样翻转数十种参考? Is this simply a matter of testing subject matter comprehension, or is this 'reference flip-flopping' a practice used in "everyday" development? 这仅仅是测试主题理解能力的问题,还是“日常”开发中使用的“参考触发器”?

using System;

namespace QuickConsoleTesting
{
   class Program
   {
      static void Main()
     {
         //Instantiate two new Person objects on the heap
         Person person1 = new Person() { Name = "Jim" };
         Person person2 = new Person() { Name = "Todd" };
         Person person3 = person1;
         Person person4 = new Person() { Name = "Julie" };

         //Flip-flop reference variables
         person4 = person2;
         person1 = person3;

         //Display results
         Console.WriteLine(person1.Name);
         Console.WriteLine(person2.Name);
         Console.WriteLine(person3.Name);
         Console.WriteLine(person4.Name);

        /* ======================================
         * Results displayed: Jim, Todd, Jim, Todd
         * ===================================== */

        //Hold console window
        Console.Read();

      }//END OF MAIN
    }//END OF PROGRAM

    class Person
    {
       private string name = "";

       public string Name
       {
          get { return name; }
          set { name = value; }
       }
    }//END OF PERSON

 }//END OF NAMESPACE

These sort of questions are there to test if a candidate understands the difference between a reference variable and a value variable. 这些问题可以用来测试候选人是否理解参考变量和值变量之间的差异。 The supplied scenario (as is) will almost never happen in Real World (tm) code. 提供的方案(按原样)几乎不会在真实世界(tm)代码中发生。 But, confusion resulting in not understanding why this code snippet does what it does can and does result in a lot of bugs, especially when there are various functions working on the same data. 但是,混淆导致无法理解为什么此代码片段会执行其功能,并且会导致很多错误,尤其是当有多个函数在同一个数据上工作时。

imagine a class 想象一堂课

class Person
{
pubic string Name;
}

With pass by value which is default 使用默认值传递

Person p=new Person();
//p contains the address of the newly allocated person
change(p);
//p is passed by value so the address within p would get copied into p1 which is a method parameter of change method

public void change(Person p1)
//p1 and p are separate variables containing address of person object
{
    p1.Name="SO";
    //changes name of p1 and p
    p1=null;
    //this makes p1 null not p since p and p1 are separate copies pointing to person object
}

With reference 供参考

change(ref p);
//p is now passed by reference so p1 is p
public void change(ref Person p1)//p1 and p are same
{
    p1.Name="SO";
    p1=null;//this makes p and p1 null
}

I am not sure exactly what you're asking, there are many reasons a developer might store the same reference in multiple variables, but that all depends on the code. 我不确定您要问的是什么,有很多原因使开发人员可能将相同的引用存储在多个变量中,但这全都取决于代码。 Remember there is still only one place that the object lives on in the heap, but could have many references to it (and thats where Managed Garbage Collector comes in to clean up objects no longer referenced) 请记住,对象仅存在于堆中的一个地方,但是可以有很多引用(这就是托管垃圾收集器用来清理不再引用的对象的地方)

Obviously your example is simply bad code. 显然,您的示例只是错误的代码。 These types of questions are akin to the C++ pointer questions. 这些类型的问题类似于C ++指针问题。 C# as a manged language has tried to stay away from pointers, but it is still important to understand the differences between value types and reference types. C#作为一种托管语言试图避免使用指针,但是了解值类型和引用类型之间的差异仍然很重要。 This is mostly what these questions are designed for. 这些主要是针对这些问题的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM