Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If Group is a custom data type you can overload the <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>=</code>, <code>==</code>, <code>!=</code> and subsequently <code>+=</code>, <code>-=</code>, <code>*=</code>, and <code>/=</code> operators as shown here: <a href="http://msdn.microsoft.com/en-us/library/aa288467%28v=vs.71%29.aspx" rel="nofollow">MSDN: Operator Overloading Tutorial</a></p> <p>If your data type is a native data type: <code>int</code> (<code>Int32</code>), <code>long</code>, <code>decimal</code>, <code>double</code>, or <code>float</code> you can do the operations you have. </p> <p>To output the summation of your numbers you can use:</p> <pre><code>String.Format("{0} + {1} = {2}", Group1, Group2, (Group1 + Group2)); </code></pre> <p>or</p> <pre><code>String.Format("{0} + {1} + {2} = {3}", Group1, Group2, Group3, (Group1 + Group2 + Group3)); </code></pre> <p>Finally if in those examples Group is a custom data type, you would also have to overload the <code>ToString()</code> method so that it can display properly.</p> <pre><code>&lt;bleepzter/&gt; </code></pre> <p>OK, Part 2 - OO Algorithm Design?</p> <p>So lets say you have the following:</p> <pre><code>public class Set: List&lt;float&gt; { public Set():base(){} public static Set operator+(Set set1, Set set2) { Set result = new Set(); result.AddRange(set1.ToArray()); result.AddRange(set2.ToArray()); return result; } public float Sum { get { if( this.Count == 0 ) return 0F; return this.Sum(); } } public override string ToString() { string formatString = string.Empty; string result = string.Empty; for(int i=0; i&lt;this.Count; i++) { formatString += "{" + i.ToString() + "} + "; } formatString = result.TrimEnd((" +").ToCharArray()); // remove the last "+ "; float[] values = this.ToArray(); result = String.Format(formatString, values); return String.Format("{0} = {1}", result, this.Sum); } } </code></pre> <p>The object Set will have a <code>Sum</code> property, as well as a <code>ToString()</code> method that will display the sum and all of its content.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload