Click here to Skip to main content
15,919,434 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
namespace WorkingOut
{
    interface IColor
    {

        
        void Getcolor();
         string display();


    }
    class Car:IColor
    {

        public string carName
        {
            get;
            set;
        }
        public Car(string CarName)
        {
          this.carName=CarName;

        }
            public string Describe()
            {

                return "the name of the car is "+carName+"\n colour of the car: "+color;
            }



            public string display()
            {
                return "colour of the car is "+color;
            }

            public string color
            {
                get;
                set;

            }

            public void Getcolor()
            {
                Console.WriteLine("ENTER THE color of the car "+carName);
                color = Console.ReadLine();


            }
    }
    class Program
    {
        static void Main(string[] args)
        {
            
            char ans='y';
            List<Car> Cab = new List<Car>();
            while ((ans) != 'n' || (ans) != 'n')
            {
                Console.WriteLine("enter the cab name");
                Cab.Add(new Car(Console.ReadLine()));
                Console.WriteLine("wanna continue? press N to quit, Y to continue");
                ans=Convert.ToChar( Console.ReadLine());
                
            }
            
            foreach(Car c in Cab)
            {
                
                c.Getcolor();
                c.display();
            }
            foreach (Car c in Cab)
            { Console.WriteLine(c.Describe()); }

            Console.ReadLine();
            }
    }

    }
Posted
Comments
Sascha Lefèvre 27-Apr-15 9:49am    
I don't see a non-automatic property "colour" there..?

While it's not completely clear from the code that you posted, I'm pretty certain that your issue is because of the following mistake:
C#
class StackoverflowExample
{
   // you're calling the property from itself
   // and then it will call itself again and again and again...
   // leading to a stack overflow

   public string SomeProperty
   {
      get { return SomeProperty;  } 
      set { SomeProperty = value; }
   }
}


A properly implemented non-automatic property has to have a "backing field":
C#
class PropertyWithBackingFieldExample
{
   private string SomePropertyBackingField;

   public string SomeProperty
   {
      get { return SomePropertyBackingField;  }
      set { SomePropertyBackingField = value; }
   }
}


Automatic properties actually are exactly the same - just not visible in the source code, because the compiler will implement the backing field for you automatically.
 
Share this answer
 
v2
Comments
PrawinS 27-Apr-15 10:17am    
thank u for the answer :)
Sascha Lefèvre 27-Apr-15 10:22am    
You're welcome! Please be so kind and mark the solution as accepted :)
C#
public string color
            {
                get
                {
                 return color;
                }
                set { color=value;}

            }

this is the non-automatic property. if i use this instead of the following code,

            public string color
            {
                get;
                set;

            }

, it simply throws stack overflow error.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900