C
C#2y ago
LeMixer

✅ Make set accessor of inherited prop private

I have the following interface:
public interface IItemModel
{
public int Id { get; set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
}
public interface IItemModel
{
public int Id { get; set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
}
since methods in an interface can not be private, but I want the setter to be private I get CS0277 when trying this:
internal class FoodItemModel : IItemModel
{
public int Id { get; private set; }
}
internal class FoodItemModel : IItemModel
{
public int Id { get; private set; }
}
Is there a way to make the setter private?
22 Replies
TheBoxyBear
TheBoxyBear2y ago
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; }
Shinyshark
Shinyshark2y ago
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.
TheBoxyBear
TheBoxyBear2y ago
If you define like that, it can still be accessed publicly if the object is referenced under the interface
LeMixer
LeMixerOP2y ago
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?
TheBoxyBear
TheBoxyBear2y ago
You can define it as get only in the interface and add a setter in the class
Shinyshark
Shinyshark2y ago
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?
LeMixer
LeMixerOP2y ago
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
Shinyshark
Shinyshark2y ago
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.
LeMixer
LeMixerOP2y ago
Sure, thank you!
Shinyshark
Shinyshark2y ago
public abstract class ItemModelBase
{
public int Id { get; private set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
}

public class FoodItemModel : ItemModelBase
{
public void FillerMethod()
{
Console.WriteLine("I am a filler method.");
}
}
public abstract class ItemModelBase
{
public int Id { get; private set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
}

public class FoodItemModel : ItemModelBase
{
public void FillerMethod()
{
Console.WriteLine("I am a filler method.");
}
}
Shinyshark
Shinyshark2y ago
Shinyshark
Shinyshark2y ago
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
LeMixer
LeMixerOP2y ago
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
Shinyshark
Shinyshark2y ago
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.
Shinyshark
Shinyshark2y ago
public abstract class ItemModelBase
{
protected void SetId(int id)
{
this.Id = id;
}

public int Id { get; private set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
}
public abstract class ItemModelBase
{
protected void SetId(int id)
{
this.Id = id;
}

public int Id { get; private set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
}
Shinyshark
Shinyshark2y ago
Something like this could help that though. If you provide an accesible method in the base class to change the Id.
Shinyshark
Shinyshark2y ago
That protected method is only available to the class that it inherits it.
Shinyshark
Shinyshark2y ago
So your Id is privately set in the base, managed in the inheritor and inaccessible to classes outside of that scope.
LeMixer
LeMixerOP2y ago
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 ^^
Shinyshark
Shinyshark2y ago
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.
LeMixer
LeMixerOP2y ago
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 ^^
Shinyshark
Shinyshark2y ago
No worries, you have a good day too.
Want results from more Discord servers?
Add your server