Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.StackOverflowException , when get set Properties are used?

Tags:

c#

asp.net

wcf

An unhandled exception of type 'System.StackOverflowException' occurred in wcfserviceLibrary.DLL

the code is show as follows.

[DataContract]
public class memberdesignations
{
    [DataMember]
    public string DesigId
    {
        get { return DesigId; }
        set { DesigId = value;}
    }
    [DataMember]
    public string DesignationName
    {
        get { return DesignationName; }
        set { DesignationName = value; }
    }

}

then i have method of Type memberdesignations as follows

public List<memberdesignations> memberdesignations()
    {
        List<memberdesignations> designations = new List<memberdesignations>();
        memberdesignations objmemDesignations;
        ds = objbll.Get_Member_Designations();
        DataView dv = new DataView();
        dv = ds.Tables[0].DefaultView;
        foreach (DataRowView drow in dv)
        {
            objmemDesignations = new memberdesignations();
            objmemDesignations.DesigId = drow["DesignationId"].ToString();
            objmemDesignations.DesignationName = drow["DesignationName"].ToString();
            designations.Add(objmemDesignations);
        }
        return designations;
    }

iam getting the error in the class containing the get set properties.

But i was able to get rid of the error when i modified the class like this :

 [DataContract]
public class memberdesignations
{
    [DataMember]
    public string DesigId
    {
        get;  set;
    }
    [DataMember]
    public string DesignationName
    {
        get; set;
    }
}

On searching the forum , i found the cause for it was and explained by Konamiman here

i would like to know the difference between the two different ways for properties explained by Konamiman

or any other explanation would be appreciated.

Thanks

like image 355
Mourya Avatar asked Mar 21 '12 04:03

Mourya


People also ask

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. } } }

What is system StackOverflowException in c#?

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. StackOverflowException uses the HRESULT COR_E_STACKOVERFLOW, which has the value 0x800703E9.

How do I get system StackOverflowException?

Once you have the settings open, expand 'Common Language Runtime Exceptions', expand 'System', scroll down and check 'System. StackOverflowException'. Then you can look at the call stack and look for the repeating pattern of calls.

Can we handle StackOverflowException?

NET Framework 2.0, you can't catch a StackOverflowException object with a try / catch block, and the corresponding process is terminated by default. Consequently, you should write your code to detect and prevent a stack overflow.


3 Answers

The issue is that, as Konamiman said, you are calling a property recursively.

Let's say I have a string property "DesignationName".

public string DesignationName
{
  //Some getter to return some data
  //Some setter to set the data
}

What would you expect it to return? How about returning a hard-coded string _designationName;

private string _designationName = "someName";
public string DesignationName
{
  get {return _designationName;}
  //Some setter to set the data
}

That works. But what would happen if I had it return itself,instead?

public string DesignationName
{
  get {return DesignatioName;}
  //Some setter to set the data
}

Well, it would keep calling DesignationName, which would keep calling itself again, which would again call DesignationName...and so on. All of this puts data on the stack, and goes on forever until is overruns the allocated space for the stack. Voila, a stackoverflow.

The reason your last example works is because it is using what is called an 'autoproperty', a new feature to .NET 3.0. Basically, behind the scenes, it is creating backing fields for your properties so that this:

public string DesignationName
{
  get;
  set;
}

actually compiles to behave like this:

private string _designationName = string.Empty;
public string DesignationName
{
  get { return _designationName; }
  set { _designationName = value; }
}
like image 77
Killnine Avatar answered Oct 16 '22 22:10

Killnine


You are referring to the property itself in the setter, so it will be calling itself recursively.(Over and over again until your stack overflows)

By using the short hand notation with just get; and set;, you are basically adding an implied backing field (like a backing variable). This way your not triggering a recursive calls since your property is just a wrapper around the backing field.

like image 6
TGH Avatar answered Oct 16 '22 23:10

TGH


declare private variables for both : _desigId, _designationName. You are in a recursive-loop that will go on infinitely. return the private variables, rather than the properties.

like image 5
Tanveer-Ibn- Haresh Avatar answered Oct 16 '22 23:10

Tanveer-Ibn- Haresh