Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I .Add() to a List which is a property with only a get accessor?

I'm following a tutorial on procedurally generated meshes and I am wondering why one can .Add() to a List which is a property of a class when that List only has a get accessor. I'm new to C#, what am I missing here?

For example, the tutorial has the user write this class:

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;
    }
}

Then we write another script which uses the class:

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 () {

    }
}

So why are we able to perform:

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

and why does it modify the private field m_Vertices when the property Vertices only has a get accessor?

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

Thanks for your insight.

If anyone is interested in the tutorial, I found it here:

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

like image 323
mgb Avatar asked Feb 01 '26 21:02

mgb


1 Answers

Since the Add method belongs to the List<T> class, all you need to call it is a reference to the list object. It is sufficient to have a getter to supply a reference, because you do not need to assign a new list, only to get an existing one.

If you would like to prohibit adding, change the getter to return AsReadOnly(), like this:

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

Note that the call of Add() would compile, but since at runtime the list is is read-only, an exception would be thrown when the Add() method is called.

like image 168
Sergey Kalinichenko Avatar answered Feb 04 '26 09:02

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!