Home > other >  What to use instead of value for string?
What to use instead of value for string?

Time:07-09

I have commented on the code that what should I put instead of value there as I used in Power

private int _power;
public int Power{
    get{
        return _power;
    }
    set{
        _power = value;
    }
}

private string _name;
public int Name{
    get{
        return _name;
    }
    set{
        _name = ?; // what should i put here
    }
}

CodePudding user response:

public int Name{
   get; set;
}

This removes the whole private property specification and lets you create properties faster.

Value is a language keyword, you can use it to condition whether the value is changed when you return it from the property.

I advice you to read more about how properties work.

CodePudding user response:

you need to do

_name = value;

but that will be a problem because your field _name is defined as a string but your have specified your property Name as an int.

Change Name to be a string, then set _name = value;

private string _name;
public string Name{
    get{
        return _name;
    }
    set{
        _name = value; 
    }

As others have posted, there are different ways to use properties in C# - but there's nothing wrong with the way you are doing it - just your typo where you used int instead of string (I bet you copy/pasted the Power property! :)

  • Related