由于StackOverFlowException C#,进程终止

前端之家收集整理的这篇文章主要介绍了由于StackOverFlowException C#,进程终止前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的代码.我无法弄清楚为什么这段代码给出了’由于StackOverFlowException而导致进程终止’.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SadiSDAL4
{
    class Program
    {
        static void Main(string[] args)
        {
            PrivateVehical privateV = new PrivateVehical("001");
            PrivateVehical pVehical = (PrivateVehical)privateV.Clone();
            Console.WriteLine("Cloned : {0}",privateV.name);
            Console.WriteLine("Cloned : {0}",pVehical.name);
            privateV.name = "Sadia's Car";
            Console.WriteLine("Cloned : {0}",pVehical.name);
            pVehical.name = "Waheed's Car";
            Console.WriteLine("Cloned : {0}",pVehical.name);
            Console.WriteLine(privateV.GetHashCode().ToString());
            Console.WriteLine(pVehical.GetHashCode().ToString());
            PublicVehical publicV = new PublicVehical("002");
            Console.WriteLine(publicV.id);
            PublicVehical pubVehi = (PublicVehical)publicV.Clone();
            Console.WriteLine("Cloned : {0}",pubVehi.id);
        }
    }
    abstract class Vehical
    {
        private string vehicalId = "01";
        public string name = "Car_1";
        public Vehical(string id)
        {
            this.vehicalId = id;
        }
        public string id
        {
            get { return id; }
            set { this.vehicalId = id; }
        }
        public abstract Vehical Clone();
    }
    class PrivateVehical : Vehical
    {
        public PrivateVehical(string id)
            : base(id)
        {

        }
        public override Vehical Clone()
        {
            return (Vehical)this.MemberwiseClone();
        }
    }
    class PublicVehical : Vehical
    {
        public PublicVehical(string id)
            : base(id)
        {

        }
        public override Vehical Clone()
        {
            return (Vehical)this.MemberwiseClone();
        }
    }
}

这是输出.

有人能解释一下它的原因是什么吗?为什么它在第一部分和第一部分工作不在另一个?

@H_404_9@解决方法
看看这段代码
public string id
    {
        get { return id; }
        set { this.vehicalId = id; }
    }

具体来说,获取{return id; }.您将属性返回给自己,导致SO错误.

这是你的意图:

public string id
    {
        get { return this.vehicalId; }
        set { this.vehicalId = value; }
    }
原文链接:https://www.f2er.com/csharp/99159.html

猜你在找的C#相关文章