I'm new to C# and a little confused about passing parameters to class properties. In the code below I have created two classes, 'AddUpMethod' and 'AddUpProperties' which both add two numbers. AddUpMethod passes the values 100 & 200 after the object is created and the method 'AddUpNumbers()' performs the addition returning the result to the variable 'totalOne'.
In the secong class 'AddUpProperties' I pass the values 100 and 200 to the constructor 'AddUpProperties(int n1, int n2)' when the object is created. The addition is performed in the result returned to 'totalTwo'.
Both examples return the result from read only variables and the fields are private.
My question is this. Which method of creating objects and passing parameters is correct (If thats the right word) or am I missing something ? I've heard the class' AddUpMethod' called 'Traditional Class Field Access'. Is this correct ? Please could someone advise me as to which technique to use.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Accessors
    class Program
        static void Main()
            AddUpMethod addUpClassFieldAccess = new AddUpMethod();

            AddUpProperties addUpProperties = new AddUpProperties(100, 200); // Parameters for propeties

            int totalOne = addUpClassFieldAccess.AddUpNumbers(100, 200); // Properties using 'Class Field Access'

            int totalTwo = addUpProperties.AddTwoNumbers; // Properties using 'Get'

            Console.WriteLine("Add Two Numbers using 'CLASS FIELD ACCESS' add up to {0}", totalOne);

            Console.WriteLine("Add Two Numbers using 'GET' only add up to {0}", totalTwo);


    class AddUpMethod
        private int _total; // Field

        //An Example of Traditional Class Field Access ?
        public int AddUpNumbers(int number1, int number2)
            _total = number1 + number2;
            return _total;

    class AddUpProperties
        private int number1; // Fields
        private int number2;
        private int total; // Read only field

        public AddUpProperties(int n1, int n2) // Constructor with two Parameters
            this.number1 = n1;
            this.number2 = n2;
        public int AddTwoNumbers 
                this.total = (this.number1 + this.number2);
                return total;



Neither way is wrong; either one is a valid method for passing values to a new object.
Which one you use depends more on the situation.
For instance, if you have a value that is manadatory or required whilst instantiating the object then placing it in the constructor ensures that it is passed when the object is created.
If you plan on passing the value to the object and then retrieving it later then a property is better sicne you can set the get and set methods to allow it to be read in and out of the object.

This article has been dead for over six months. Start a new discussion instead.