Aku
Aku

Reputation: 200

C# StackOverflowException when use getter

I have getter which changes the first letter of a string to capital. But I get StackOverflowException.

namespace ConsoleApplication1
{
    class Program
    {
        class Human
        {     
            public String Name
            {
                get
                {

                    char[] letters = Name.ToCharArray();
                    // upper case the first char
                    letters[0] = char.ToUpper(letters[0]);
                    // return the array made of the new char array
                    return new String(letters);
                    //return Name.First().ToString().ToUpper() + String.Join("", Name.Skip(1));
                }
                set
                {
                
                }
            }

What did I do wrong?

Upvotes: 0

Views: 361

Answers (5)

Martin Mulder
Martin Mulder

Reputation: 12934

Your property Name is calling itself in line char[] letters = Name.ToCharArray();. You will have to use a field instead of the property:

class Human
{     
    private string _name;

    public String Name
    {
        get
        {
            if (_name == null)
                return null;
            if (_name.Length <= 1)
                return _name.ToUpper() + _name.Substring(1);
            return _name.Substring(0, 1).ToUpper() + _name.Substring(1);
        }
        set
        {
            _name = value;
        }
    }
}

I also took the freedom to try to make your function work with the upper casing. Or in my opinion even better:

class Human
{     
    private string _name = "";

    public String Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (String.IsNullOrEmpty(_name))
                _name = value;
            else  
                _name = value.Substring(0, 1).ToUpper() + _value.Substring(1);
        }
    }
}

Upvotes: 4

Martin
Martin

Reputation: 2300

Here's a working Name method, which makes the first letter of the sentence uppercase, when you get the value.

private string _name;
public string Name
{
    get
    {
        if (string.IsNullOrEmpty(_name))
        {
            return _name;
        }
        var charArray = _name.ToCharArray();
        charArray[0] = char.ToUpper(charArray[0]);
        return new string(charArray);
    }
    set { _name = value; }
}

Upvotes: 0

Geeky Guy
Geeky Guy

Reputation: 9399

This:

char[] letters = Name.ToCharArray();

When you try to read something from Name, you're using the getter. Thus you've caused an infinite loop.

Upvotes: 1

Kenneth
Kenneth

Reputation: 28747

That's because you are accesing the getter inside the getter.

You should create a backing variable and access that variable instead of the Name:

class Human
    {     
    private string _name = string.Empty;    
    public String Name
        {
            get
            {

                char[] letters = _name.ToCharArray();  // use the backing variable instead of Name
                // upper case the first char
                letters[0] = char.ToUpper(letters[0]);
                // return the array made of the new char array
                return new String(letters);
                //return Name.First().ToString().ToUpper() + String.Join("", Name.Skip(1));

                return new String(;// ToUpperFirstLetter(this.Imie);
            }
            set
            {
                _name = value;
            }
        }

Upvotes: 2

I4V
I4V

Reputation: 35363

This line char[] letters = Name.ToCharArray(); calls recursively the property public String Name

Upvotes: 6

Related Questions