---
tags:
    - "Add"
    - "Total"
    - "Accumulate"
    - "Aggregate"
    - "List"
    - "Array"
---

# Sum

[Collections](index.md) / **Sum**

<div class="grid cards" markdown>

* :green_circle: Can Read

* :red_circle: Cannot Write

</div>

## Description

Adds up every item of a collection read as a number

## Parameters

| Name | Description |
| :--- | :---------- |
| Collection | The collection whose items are added up |

## Code

```cs
using System;
using UnityEngine;
using VisualScript.Icons;

namespace VisualScript.Runtime
{
    [Get]

    [Title("Sum")]
    [Category("Collections/Sum")]

    [Description("Adds up every item of a collection read as a number")]
    [Image(typeof(IconSigma), ColorTheme.Type.Teal)]

    [Parameter("Collection", "The collection whose items are added up")]

    [Keywords("Add", "Total", "Accumulate", "Aggregate", "List", "Array")]

    [Serializable]
    public class TypeNumberCollectionSum : TypeNumber
    {
        [SerializeField] private GetCollection m_Collection = new GetCollection();

        public override double Get(Args args)
        {
            using Args scoped = Args.Get(args);

            int count = this.m_Collection.Capture(scoped);
            double sum = 0f;

            for (int i = 0; i < count; ++i)
            {
                if (ArgsCollectionParameters.TryGetItem(this.m_Collection, scoped, i, out double item))
                {
                    sum += item;
                }
            }

            return sum;
        }

        public override string ToString()
        {
            return $"Sum of {this.m_Collection}";
        }

        protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
    }
}

```

