进程终止堆栈溢出异常 [英] Process is terminated stack overflow exception

查看:78
本文介绍了进程终止堆栈溢出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 命名空间  ConsoleAccessPrivateMembers  
{
class MyPrivateClassMem
{
private int a = 10, b = 20;

public int aa
{
get
{
return aa;
}
set
{
aa = a ;

}
}
public int bb
{
get
{
return bb ;
}
set
{
bb = b ;

}
}
public int add()
{

return aa + bb;
}




}
class MyInput
{

static void Main(string [] args)
{
MyPrivateClassMem obj = new MyPrivateClassMem();
obj aa < span class =code-leadattribute> = 1;
obj bb = 2;


控制台 .WriteLine(obj.add());


}
}
}





我正在尝试使用更改私有变量值属性但在执行时由于stackoverflow异常而导致进程终止错误

所以,请在此帮助。

解决方案

从C#3.0开始,您现在已经自动实现了属性,以前不再需要标准使用后备字段变量。如果你写这段代码:

 public int a {get;组; } 

编译器会为你生成一个隐藏的私有变量。



这是你的代码可能看起来像利用这一点:

 使用系统; 

命名空间 ConsoleAccessPrivateMembers
{
internal class MyPrivateClassMem
{
// 无参数ctor
public MyPrivateClassMem()
{
a = 10 ;
b = 20 ;
}

// 提供参数的ctor
public MyPrivateClassMem( int parametera, int 参数b)
{
a = parametera;
b = parameterb;
}

// 自动实施的公共属性
public int a { get ; set ; }
public int b { get ; set ; }

public int add ()
{
return a + b;
}
}

内部 MyInput
{
私有 静态 void Main( string [] args)
{
var obj1 = new MyPrivateClassMem();
obj1.a = 1 ;
obj1.b = 2 ;
Console.WriteLine( obj1 add result: + obj1。添加());

var obj2 = new MyPrivateClassMem( 100 200 );
Console.WriteLine( obj2 add result: + obj2。添加());

// 保持控制台窗口打开
Console.ReadLine ();
}
}
}

注意使用无参数的构造函数,它允许将'a和'b初始化为默认值。



另请注意第二个可选的构造函数,其中提供了参数,因此,如果您愿意,可以在创建类'MyPrivateClassMem的实例时初始化'a和'b。


getter应返回局部变量中的值,setter应使用关键字'value'来查看私有变量的值。



  public   int  aa 
{
get
{
return a;
}
set
{
a = value ;

}
}
public int bb
{
get
{
return b;
}
set
{
b = value ;

}
}


namespace ConsoleAccessPrivateMembers
{
    class MyPrivateClassMem
    {
       private int a = 10, b = 20;

       public int aa
       {
           get
           {
               return aa;
           }
           set
           {
               aa = a;

           }
       }
       public int bb
       {
           get
           {
               return bb ;
           }
           set
           {
               bb = b;

           }
       }
        public int add()
        {

            return aa+bb ;
        }




    }
    class MyInput
    {

        static void Main(string[] args)
        {
            MyPrivateClassMem obj = new MyPrivateClassMem();
            obj.aa = 1;
            obj.bb = 2;


            Console.WriteLine(obj.add());


        }
    }
}



I am trying to change private variables values using properties but at executing time "Process is terminated due to stackoverflow exception " error encountered
So ,Kindly help here.

解决方案

Beginning with C# 3.0, you now have auto-implemented Properties where what used to be the standard use of a "backing-field" variable is no longer necessary. If you write this code:

public int a { get; set; }

The compiler will generate a hidden private variable for you.

Here's what your code might look like taking advantage of this:

using System;

namespace ConsoleAccessPrivateMembers
{
    internal class MyPrivateClassMem
    {
        // parameterless ctor
        public MyPrivateClassMem()
        {
            a = 10;
            b = 20;
        }

        // ctor with supplied parameters
        public MyPrivateClassMem(int parametera, int parameterb)
        {
            a = parametera;
            b = parameterb;
        }

        // auto-implemented public properties
        public int a { get; set; }
        public int b { get; set; }

        public int add()
        {
            return a + b;
        }
    }

    internal class MyInput
    {
        private static void Main(string[] args)
        {
            var obj1 = new MyPrivateClassMem();
            obj1.a = 1;
            obj1.b = 2;
            Console.WriteLine("obj1 add result: " + obj1.add());

            var obj2 = new MyPrivateClassMem(100, 200);
            Console.WriteLine("obj2 add result: " + obj2.add());

            // keep the Console window open
            Console.ReadLine();
        }
    }
}

Note the use of a parameterless Constructor that allows for the initialization of 'a and 'b to default values.

Also note a second, optional, Constructor with parameters supplied, so you can initialize 'a and 'b, if you wish, when you create an instance of the Class 'MyPrivateClassMem.


The getters should return the value in the local variable and the setters should use the keyword 'value' to se the values of the private variables.

public int aa
       {
           get
           {
               return a;
           }
           set
           {
               a = value;

           }
       }
       public int bb
       {
           get
           {
               return b ;
           }
           set
           {
               b = value;

           }
       }


这篇关于进程终止堆栈溢出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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