c# - Change Private Property of Class -
my person class:
class person { public string firstname { get; private set; } public string lastname { get; private set; } public int age { get; private set; } public person(string firstname,string lastname,int age) { this.firstname = firstname; this.lastname = lastname; this.age = age; } public override string tostring() { return this.firstname + " " + this.lastname + " " + this.age; } }
main:
class program { static void main(string[] args) { person sallyperson = new person("sally", "solomon",23); } }
lets want change firstname , age of person, how go doing so? firstname , age properties set privately.
so realized, of answers assuming question you're asking literally question you're asking, rather example. perhaps were using example, , need set private property of object don't control source of? in case, say, that's bad idea, if need anyway, there way. way called "reflection" (this being 1 of many kind-of-sketchy things can reflection if want). this question might want at, if you're after.
more likely, though, correct response don't have properties private if control source class, , going want change them.
Comments
Post a Comment