Problem using a class that always returns 0 regardless of the value

Viewed 70

I have created this class for complex number


        using System;
        using System.Collections.Generic;
        using System.Text;
    
    namespace borrar
    {
        public class Complejos
        {
            public Complejos()
            {
    
            }
    
            private double real;
            private double imaginario;
            public Complejos(double Real, double Imaginario)
            {
                real = Real;
                imaginario = Imaginario;
            }
            public double Real { get; set; }
            public double Imaginario { get; set; }
        }
    }

There are no error messages, but if I use the constructor with two elements, it always return 0. For instance, if I write


    class Program
        {
            static void Main(string[] args)
            {
                
                Complejos pruebas = new Complejos(1, 1);
                Console.WriteLine(pruebaas.Real);
            }
        }

Visual studio returns 0.

I do not find out the error, could somebody telle me how can I fix it? Thanks ad advance.

2 Answers
private double real;
private double imaginario;
public Complejos(double Real, double Imaginario)
{
    real = Real; // <= this sets the private var "real" to the value of the _param_ "Real".
    imaginario = Imaginario;
}


public double Real { get; set; } // <= This is an autoproperty that has 
                                 //    NOTHING TO DO with "real"
                                 //    and is initialized with default(double), which is 0.0
public double Imaginario { get; set; }

So, since field real and property Real are not related in any way, you will get the default value of a double-property when you read-access Real like in your example.

Two ways of fixing this:

  1. Using a field-backed property instead of auto
     public double Real {
          get { return real; }
          set { real = value;}
     }
  1. Using the autoproperty in the CTOR and dropping the private field.
public Complejos(double Real, double Imaginario)
    {
        this.Real = Real; // <= this sets the property "Real" to the value of the _param_ "Real".
        this.Imaginario = Imaginario;
    }

I'd chose option 1. only if I had a very good reason to do so. Basically, the AutoProperty will create a private field "under the hood" anyway.

You should return the backing variable when the Property is called like this

public class Complejos
    {
        public Complejos()
        {

        }

        private double real;
        private double imaginario;
        public Complejos(double Real, double Imaginario)
        {
            real = Real;
            imaginario = Imaginario;
        }
        public double Real
        {
            get
            {
                return real;
            }
            private set //make it private if no one other than class can set the value for real
            {
                real = value;
            }

        }
        public double Imaginario { get; set; }
    }
Related