✅ Make set accessor of inherited prop private
I have the following interface:
since methods in an interface can not be private, but I want the setter to be private I get
CS0277
when trying this:
Is there a way to make the setter private?22 Replies
Can't be done because the setter is defined in the interface
Unless you were to define the property explicitely for the interface
int IItemModel.Id { get; set; }
Interface cannot declare private properties or methods. An interface is meant to be a contract for classes approaching it from the outside.
Declaring privates in your interfaces is very counterintuitive in that regard.
If you define like that, it can still be accessed publicly if the object is referenced under the interface
Okay makes sense, but I still the usecase is still there, right? Having a BaseItemModel, and making the props of inherited members readonly
Do I just use a class instead?
You can define it as get only in the interface and add a setter in the class
You can. Look at it like this though. When you put an interface onto a class and approach that class via the interface, the interface offers you everything on a generic level. You are no longer concerned with the concrete implementation of the class but rather about what all classes of this interface share.
But when you attempt to put private properties or methods into the interface, you are saying "Hello I am offering this private property / method to you, but you cannot use it."
Additionally you could use a different setter
What is the use case for you? Why do you want your setter to be private?
I think that makes sense. I still have problems grasping the concept of interfaces. I have a lot of different item types and new ones might be added later. So I want a BaseItemClass, so I can use them all in a list e.g. and to make implementing new item types easier
Interfaces can be very useful but they are widely used by people because others do it. A lot of understanding behind interfaces comes from use cases where they were more of an asset than just boilerplate.
Give me a few minutes, I think I know something that could help you.
Sure, thank you!
I don't know if this is what you were looking for, but you could additionally use a class like you said
But if you mark that class as abstract, the class itself cannot be instantiated. You specifically mark it as a class meant to be inherited from.
You can then use a private set.
It offers mostly the same benefits of using an interface: you still get to talk to the abstraction in between your code and the actual implementation.
That is to say; you are not concerned with how a FoodItemModel works or what it does. You just want to interact with an ItemModelBase
That sounds exactly like what I need. Just using a class would have been my next choice, but I did not know about abstract until now and I wanted to use an interface just to practice using them, cause I rarely seem to find a usecase for them
When you start learning about abstraction more, interfaces and abstract classes will start to make more sense.
They have specific use cases that are not required to write code but will significantly improve its maintainability if applied correctly.
Do note though that with the code I gave you above, you can only set your Id in the abstract class
even in the FoodItemModel it is private.
Something like this could help that though.
If you provide an accesible method in the base class to change the Id.
That protected method is only available to the class that it inherits it.
So your Id is privately set in the base, managed in the inheritor and inaccessible to classes outside of that scope.
Thank you a lot. Most of my projects have been the type of project you program for a couple days/weeks, maybe update some days later, but that was mostly it. So maintainability has never been a big concern for me, but I think it is time to actually worry about it ^^
It's good to think about it but if you know some co-workers / classmates that have more experience in this you could ask them to show you an example of why you would do things in these ways
They seem like they are a lot more work for little to no result.
That seems to be very useful, thank you
Yeah, you're right. I think that's a very approachable way of learning about abstraction
Thank you again for helping me and have a good evening/day depending on your time zone ^^
No worries, you have a good day too.