简体   繁体   English

C#-数组新知识,需要澄清

[英]C# - New to arrays, need clarification

namespace Program
{
    class Path
    {
      private readonly MapLocation[] _path; 

      public Path(MapLocation[] path)
       {
          _path = path;
       } 
    }
}

I am having trouble understanding _path = path; 我在理解_path = path;时遇到问题_path = path; How does one go about understanding how this works and why it's needed? 如何去了解它是如何工作的以及为什么需要它?

class Path
{
  private readonly MapLocation[] B; 

  public Path(MapLocation[] A)
   {
      B = A;
   } 
}

Basically you are assigning the array path that is passed in by the constructor to the variable _path, in the example above I have renamed them A and B , so it is assigning the array of maplocations from the constructor to the object variable ( B ) for storage. 基本上,您是将构造函数传递的数组路径分配给变量_path,在上面的示例中,我将它们重命名为AB ,因此它是将映射位置数组从构造函数分配给对象变量( B ),存储。

There are a few things you need to understand here so let's take it one step at a time. 您需要在这里了解几件事,所以让我们一次迈出一步。

Value Types vs Reference Types 值类型与引用类型

Value Types: 值类型:

In C#, some things are passed by value while others are passed by reference. 在C#中,某些事物是通过值传递的,而其他事物是通过引用传递的。 Here is an example of something passed by value: 这是按值传递的示例:

class Path
{
   private int _number; 

   public Path(int number)
   {
      _number = number;
   } 
}

Here is how we would use the above class: 这是我们如何使用上面的类:

int number = 5;
Path p1 = new Path(number);

The variable number is passed by value. 变量number按值传递。 What that means is: A copy of the value of what is stored in number is passed to the p1 object. 这意味着:将number存储的值的副本传递给p1对象。 Therefore, if you change the value of it within the Path class, the value of number which we pass will not change. 因此,如果您在Path类中更改它的值,我们传递的number的值将不会更改。 It will only change within p1 object. 它只会在p1对象内更改。 Please read more on C# value types . 请阅读有关C#值类型的更多信息

Reference Types: 参考类型:

Arrays are one of the things which are passed by reference in C#. 数组是在C#中通过引用传递的事物之一。 Here is an example of that. 这是一个例子。 I will explain readonly later on: 稍后我将解释readonly

class Path
{
   private readonly MapLocation[] _path; 

   public Path(MapLocation[] path)
   {
      _path = path;
   } 
}

Here is how we would use the above class: 这是我们如何使用上面的类:

// 2 is max number of items for this array
MapLocation[] locations = new MapLoations[2];  
locations[0] = new MapLocation();
Path p1 = new Path(locations);

The variable locations is passed by reference. 变量locations通过引用传递。 What that means is: A reference to the memory location of where locations is stored, is passed to the p1 object. 这也就意味着:到的存储器位置的参考locations被存储,传递给p1对象。 Therefore, if you change the value of it within the Path class, for example, if you add another item to the array, you will notice the change in locations . 因此,例如,如果您在Path类中更改了它的值,例如,如果向数组中添加了另一个项目,则会注意到locations的更改。 Why? 为什么? Because you passed a reference to the memory location of locations and thus whoever (for example Path ) has access to it, they can change things at that location. 因为你传递给的存储位置的参考locations ,因此无论谁(例如Path )的访问它,就可以在该位置改变的事情。 Please read more on C# Reference types . 请阅读有关C#参考类型的更多信息 So what is happening is we pass the reference of locations to the Path constructor, and within the constructor it is assigned to the _path variable. 因此,发生的事情是我们将locations的引用传递给Path构造函数,并且在构造函数中将其分配给_path变量。

The easiest way to understand reference types is to imagine you are passing a rope which is tied to the memory location. 理解引用类型的最简单方法是想象您正在传递一条绑在内存位置的绳索。 Anyone who has the rope, can find their way to the memory location and change it . 有绳索的任何人都可以找到通往存储位置的方式并进行更改 So in our example we create a variable named locations and then we tie a rope to it. 因此,在我们的示例中,我们创建了一个名为locations的变量,然后将绳子绑在它上面。 We pass the other end of the rope to the Path constructor, and within the constructor the rope is passed to the _path variable. 我们将绳索的另一端传递给Path构造函数,然后在该构造函数中将绳索传递给_path变量。 Now from within the class you can use the rope called _path and find the memory location all the way to locations . 现在,在该类中,您可以使用名为_path的绳索并_path找到内存locations This rope can span many classes and methods and so on. 这条绳索可以跨越许多类和方法,等等。 Anyhow enough with the rope. 无论如何用绳索。

So what is the readonly for? 那么, readonly做什么用的呢? This means that assignment to the variable can only happen from within the constructor and nowhere else. 这意味着对变量的赋值只能在构造函数内部进行,而不能在其他地方进行。 So in your example, _path can only be assigned to from within the constructor of the class where it appears which is Path in this case. 因此,在您的示例中,只能从出现的类的构造函数(在本例中为Path中将_path分配给。 But you ask why would someone want to do this? 但是您问为什么有人要这样做? Well sometime you want to create a class, assign something to the variable, and then you never want to allow anyone to assign to it again. 好吧,有时候您想创建一个类,为该变量分配某些内容,然后再也不想让任何人再次为其分配该变量。

Hope that helps. 希望能有所帮助。

Path is a class. Path是一类。 Outside of the class, you can create one or more objects of type Path . 在类之外,您可以创建一个或多个Path类型的对象。

Each Path object has a private reference to an array of MapLocation objects. 每个Path对象都有对MapLocation对象数组的私有引用。 This array reference is called _path . 该数组引用称为_path

When a Path object is constructed, another reference to an array of MapLocation objects is passed into the Path() constructor as an argument called path . 构造Path对象时,对MapLocation对象数组的另一个引用作为称为path的参数传递到Path()构造函数中。 Probably the expectation is that the referenced array already contains some MapLocation objects. 可能期望所引用的数组已经包含一些MapLocation对象。

The purpose of the line of code you're asking about is to make _path reference the same MapLocation array as path does. 您要查询的代码行的目的是使_path引用与path相同的MapLocation数组。 So that the new instance of Path that's getting created will have be able to access/use that array. 这样,要创建的Path的新实例将能够访问/使用该数组。

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

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