Maybe I am overlooking something obvious, but I have seen in code where you may have a property like “HairColor”, and then a method like “HairColor.Update()”. Is this possible?
Person person = new Person(int personID);
person.HairColor = "Blonde";
person.HairColor.Update();
I have specific properties that I want to be able to extend on a case by case basis. I guess I could have a method called “HairColorUpdate”, but seems that HairColor.Update() should be possible. I don’t want to use “set” because I don’t always want to update the DB this way.
The reason I am doing this is because I may only want to call the database to update one column instead of calling my save method which updates every column hopefully improving efficiency.
person.HairColor.Update()just means that the type returned by theHairColorproperty has a method calledUpdate. In your example it looks likeHairColoris astring, so to achieve that you need to implement an extension method forstring. E.g. something likeHowever, I don’t see the point of doing this. The
Updatemethod is a static method that works on the string, so it doesn’t affect thePersoninstance. (even ifstringdid have anUpdatemethod it would be unrelated to thePersontype).I believe you would want to have the
Updatemethod onPersoninstead as others have pointed out.