为什么我可以.Add()到一个只有一个get访问器的属性列表?

时间:2013-10-04 02:01:31

标签: c# unity3d

我正在关注程序生成的网格教程,我想知道为什么人们可以将.Add()添加到List中,当List只有一个get访问器时,它是一个类的属性。我是C#的新手,我在这里缺少什么?

例如,本教程让用户编写此类:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MeshBuilder
{
    private List<Vector3> m_Vertices = new List<Vector3>();
    public List<Vector3> Vertices { get { return m_Vertices; } }

    private List<Vector3> m_Normals = new List<Vector3>();
    public List<Vector3> Normals { get { return m_Normals; } }

    private List<Vector2> m_UVs = new List<Vector2>();
    public List<Vector2> UVs { get { return m_UVs; } }

    private List<int> m_Indices = new List<int>();

    public void AddTriangle(int index0, int index1, int index2)
    {
        m_Indices.Add(index0);
        m_Indices.Add(index1);
        m_Indices.Add(index2);
    }

    public Mesh CreateMesh()
    {
        Mesh mesh = new Mesh();

        mesh.vertices = m_Vertices.ToArray();
        mesh.triangles = m_Indices.ToArray();

        //Normals are optional. Only use them if we have the correct amount:
        if (m_Normals.Count == m_Vertices.Count)
            mesh.normals = m_Normals.ToArray();

        //UVs are optional. Only use them if we have the correct amount:
        if (m_UVs.Count == m_Vertices.Count)
            mesh.uv = m_UVs.ToArray();

        mesh.RecalculateBounds();

        return mesh;
    }
}

然后我们编写另一个使用该类的脚本:

using UnityEngine;
using System.Collections;

public class ProcQuad : MonoBehaviour {

    float m_Width = 1;
    float m_Length = 1;

    // Use this for initialization
    void Start () {

        MeshBuilder meshBuilder = new MeshBuilder();

        //set up vertices and triangles
        meshBuilder.Vertices.Add (new Vector3(0.0f, 0.0f, 0.0f));
        meshBuilder.UVs.Add(new Vector2(0.0f, 0.0f));
        meshBuilder.Normals.Add(Vector3.up);

        meshBuilder.Vertices.Add(new Vector3(0.0f, 0.0f, m_Length));
        meshBuilder.UVs.Add(new Vector2(0.0f, 1.0f));
        meshBuilder.Normals.Add (Vector3.up);

        meshBuilder.Vertices.Add(new Vector3(m_Width, 0.0f, m_Length));
        meshBuilder.UVs.Add(new Vector2(1.0f, 1.0f));
        meshBuilder.Normals.Add(Vector3.up);

        meshBuilder.Vertices.Add (new Vector3(m_Width, 0.0f, 0.0f));
        meshBuilder.UVs.Add(new Vector2(1.0f, 0.0f));
        meshBuilder.Normals.Add(Vector3.up);

        meshBuilder.AddTriangle(0, 1, 2);
        meshBuilder.AddTriangle(0, 2, 3);


        MeshFilter filter = GetComponent<MeshFilter>();

        if (filter != null)
        {
            filter.sharedMesh = meshBuilder.CreateMesh();           
        }

    }

    // Update is called once per frame
    void Update () {

    }
}

那么我们为什么能够表演:

meshBuilder.Vertices.Add (new Vector3(0.0f, 0.0f, 0.0f));

为什么当属性Vertices只有get访问器时它会修改私有字段m_Vertices?

public List<Vector3> Vertices { get { return m_Vertices; } }

感谢您的见解。

如果有人对本教程感兴趣,我在这里找到了它:

http://jayelinda.com/wp/modelling-by-numbers-part-1a/

3 个答案:

答案 0 :(得分:3)

由于Add方法属于List<T>类,因此您需要调用它只是对列表对象的引用。使用getter提供引用就足够了,因为您不需要分配新列表,只需要获取现有列表。

如果您想禁止添加,请将getter更改为AsReadOnly(),如下所示:

public List<Vector3> Vertices { get { return m_Vertices.AsReadOnly(); } }

请注意,Add()的调用将编译,但由于在运行时列表是只读的,因此在调用Add()方法时会抛出异常。

答案 1 :(得分:1)

你不能set列表的事实意味着这会给你一个编译错误:

meshBuilder.Vertices = new List<Vector3>();  //Not allowed! You're assigning to a list that you can't set

但是根据你在那里的代码,get列表m_Vertices并向其添加项目(就像其他任何List一样)是完全合法的,因为你不是将新List分配给Vertices

答案 2 :(得分:1)

get访问者返回对List<T>的引用。获得对此的引用后,您可以调用List<T>上的任何方法。

缺少set访问者会阻止您替换另一个 List<T> {/ 1}}。这是List或Collection属性的常见做法,以确保这些属性为never return null