为什么使用auto属性(在类Car,color属性中)不会抛出任何堆栈溢出异常,而同一属性(颜色)不是自动抛出错误。 [英] Why does the usage of auto properties( in the class Car, color property) not throw any stack overflow exception while the same property (colour) which is not automatic throws error.?

查看:81
本文介绍了为什么使用auto属性(在类Car,color属性中)不会抛出任何堆栈溢出异常,而同一属性(颜色)不是自动抛出错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();
            }
    }

    }

推荐答案

虽然不完全清楚您发布的代码,我很确定您的问题是由于以下错误:

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:
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":

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.


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.


这篇关于为什么使用auto属性(在类Car,color属性中)不会抛出任何堆栈溢出异常,而同一属性(颜色)不是自动抛出错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆