deepu_verma

What will the output of the following problem

namespace rest

{

namespace csharptest

{

class Program

{

public static int Main(string[] args)

{

Test p = new Test();

p.i = 7;

System.Console.WriteLine(p.i);

System.Console.ReadLine();

return 0;

}

}

class Test

{

// private int i;

public int i

{

set

{

i = value;

}

get

{

return i;

}

}

}

}

}

Ans: Stackoverflowexception at i = value;
Question : why does i =value not throw a compile time error (CTE)




Re: Visual C# General getter setter error

John.Doe

You generate an endless recursion, which leads to a stack overflow.

public int i
{
 
set
 
{ i = value; }
 
get
 
{ return i; }
}

Here you call again the setter of your property and so on.
This should look similar to this:

private int i;

public int I
{
 
set
 
{ i = value; }
 
get
 
{ return i; }
}