0

Should most C# class members be set to private majority of the time, only in case I want other classes to modify them (then I will make them public)? I see most tutorials make all the class members public. I am beginning to think this may be bad practice.

Would like to understand general software engineering principles.

https://github.com/Apress/pro-asp.net-core-mvc/blob/master/Source%20Code%201-31/08%20-%20SportsStore/SportsStore/src/SportsStore/Models/Product.cs

Example:

public class Product {
        public int ProductID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
        public string Category { get; set; }
    }
  • Fields should be private. Properties (as you have here) are intended to expose class internals and can thus be public. Perhaps you really want those fields that can't be read/modified by other classes to be private fields? Note that when you use auto-properties (as in your example), the get/set methods and backing field are automatically created/implemented for you. – ProgrammingLlama Feb 18 '19 at 01:13
  • Possible duplicate of [What is the difference between a field and a property?](https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property) – ProgrammingLlama Feb 18 '19 at 01:13
  • Possible duplicate of [understanding private setters](https://stackoverflow.com/questions/3847832/understanding-private-setters) – mjwills Feb 18 '19 at 01:28
  • 1
    @GregThomas582 If you do not specify the accessibility `C#` will consider it to be `private` so in a way `private` is the default. – Franck Feb 18 '19 at 01:31
  • 1
    You have answered your own question, if they have no need to be public they should be private. That's it end of story, it reduces the degrees of freedom in your application – TheGeneral Feb 18 '19 at 01:33
  • If a method needs a variable, and it is not needed anywhere else, would you put it at the class level? No. Well public, private etc are for the same reason: why allow access if not needed. Have you used the `List` from .net? Imagine the `List.Count` property was public, and now think if that would cause any problems. – CodingYoshi Feb 18 '19 at 01:35
  • Variables should be scoped as tightly as possible. This quantitatively reduces complexity. You can always make it public later; the reverse is much more difficult. – John Wu Feb 18 '19 at 01:45

1 Answers1

-1

To answer your question in the context of an application...

Your example is that of a Model1. Its properties are public because:

By convention, public properties with a getter and a setter will be included in the model.2

In general, properties are public (or protected, or internal) and fields are private.

A property is meant to be exposed in some way. For example, a Model exposes some properties.

A private field is suitable for a dependency. For example, in ProductController3 from linked repo:

public class ProductController : Controller {
    private IProductRepository repository;
    // ...

    public ViewResult List(int page = 1) {
        // Use repository, only in ProductController
        // ...
    }
}

References

  1. https://learn.microsoft.com/en-us/ef/core/modeling/
  2. https://learn.microsoft.com/en-us/ef/core/modeling/included-properties
  3. https://github.com/Apress/pro-asp.net-core-mvc/blob/ecbc7336cc9b4adb05825e8a0a355f3089fa4f0a/Source%20Code%201-31/08%20-%20SportsStore/SportsStore/src/SportsStore/Controllers/ProductController.cs

P.S. Questions on "general software engineering principles" risk getting closed as primarily opinion-based. See this Stack Overflow help topic: How do I ask a good question?

aaron
  • 39,695
  • 6
  • 46
  • 102
  • -1 because you know *Questions on "general software engineering principles" risk getting closed as primarily opinion-based.*, then why answer and not close it? – CodingYoshi Feb 18 '19 at 01:48
  • @CodingYoshi Because I believe my answer adds value for readers — value that is not addressed by the proposed duplicates. I posted my answer fully aware that people like you enjoy down-voting, but I'm not here for the rep. Have a good day! – aaron Feb 18 '19 at 01:53
  • thank you Aaron, this is very helpful answer for beginners, Microsoft documentations is stating this also –  Feb 18 '19 at 02:37