Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process is Terminated due to StackOverFlowException C# [duplicate]

Tags:

c#

exception

This is my code. I am not able to figure out why this code is giving 'Process is Terminated due to 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}", privateV.name);
            Console.WriteLine("Cloned : {0}", pVehical.name);
            pVehical.name = "Waheed's Car";
            Console.WriteLine("Cloned : {0}", privateV.name);
            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();
        }
    }
}

This is the output. enter image description here Can someone explain what is the cause of it ? Why it is working in first part & not in the other ?

like image 728
LeCdr Avatar asked Mar 12 '15 18:03

LeCdr


People also ask

How do I stop StackOverflowException?

StackOverflowException is thrown for execution stack overflow errors, typically in case of a very deep or unbounded recursion. So make sure your code doesn't have an infinite loop or infinite recursion.

What causes StackOverflowException?

A StackOverflowException is thrown when the execution stack overflows because it contains too many nested method calls. using System; namespace temp { class Program { static void Main(string[] args) { Main(args); // Oops, this recursion won't stop. } } }


1 Answers

Take a look at this code:

    public string id
    {
        get { return id; }
        set { this.vehicalId = id; }
    }

Specifically get { return id; }. You are returning property to itself, causing SO error.

Is this what you intended:

    public string id
    {
        get { return this.vehicalId; }
        set { this.vehicalId = value; }
    }
like image 89
bokibeg Avatar answered Oct 06 '22 01:10

bokibeg