C # Accessors and Collections

When defining classes, I present classes as properties per row:

class ClassA { private String _Name; public String Name { get { return _Name; } set { _Name = value; } } } 

What is best used for collections within classes regarding accessories

So, if a class extends to something like:

 class ClassA { private String _Name; private List<String> _Parts = new List<String>(); public String Name { get { return _Name; } set { _Name = value; } } } 

How to open the next item?

+4
source share
8 answers

Print a read-only instance of the collection. Please note that the content is not read-only, but there is a link.

 public IList<String> Parts { get; private set; } 
+4
source

The naming conventions I came across recommend

 private String _name; 

You can also use automatic properties that generate the same code that you wrote

 public string Name {get; set;} 

For collections, I do not like to reveal the actual collection, but the methods for its work.

 public void Add(... public void Remove(... 

Otherwise, you can only do this with an automatic property.

 public IList<string> Parts {get; private set;} 
+2
source

It depends on how serious you are about how to encapsulate the way you store data. If you make an easy class, and you just provide storage, but want to completely leave the decision on access to the consumer of your class, you simply expose it as a standard property or create its auto-property.

 public List<String> Parts { get; private set; } 

If you want the variable to never be null, continue to use your private support field and add checks.

 private List<String> _Parts; public IList<String> Parts { get { if (_Parts == null) _Parts = new List<String>(); return _Parts; } private set { if (value != null) _Parts = value; } } 

If you want to control synchronization or something like that, you would expose the logical methods for what you are doing.

 public void AddPart(String part); public void RemovePart(String part); public String GetPart(int index); public IEnumerable<String> GetAllParts() { foreach(String part in _Parts) yield return part; } 
+1
source

I don’t know if there is a specific practice, but there are a few things to consider. The basic approach is the same as the others:

 public List<String> Parts { get { return _Parts; } private set { _Parts = value; } } 

It is important to note that _Parts never null . This leads to subtle and hard to reach errors.

However, if you need to send events when items are added and removed, you have only two options:

  • Use a List subclass that dispatches events when needed
  • Do not expand the list at all and simply set AddPart() , RemovePart() and ListParts() (which returns a copy of the current list).

If your needs are simple, just set the property (but don't set it to null ). Otherwise, you have to be a little more bizarre.

+1
source

Could you just do the same - but for the list?

 public List<String> parts { get { return _Parts; } set { _Parts = value; } } 
0
source

I would also display the property

 public List<string> Parts { get; set; } 
0
source

You have many options, and it really depends on what operations you want to open the public API of your class. The most common approaches:

  • Provide the readonly property to return an instance of the actual collection with information about the same type.
  • Provide a readonly property that returns an IEnumerable interface.
  • Provide the readonly property, which returns the wrapper of the ReadOnlyCollection collection.

Again, it really depends on how you want to open the collection, but the 3 options above will work fine in most scenarios. If you have more specialized requirements, such as adding add-ons to the collection from the public API, while refusing to delete, things get a little more complicated.

0
source

Usually we do the following:

 private Collection<String> _parts = new Collection<String>(); public Collection<String> Parts { get { return _parts; } } 

This ensures that the collection is created when the object is created and makes the base reference for the _parts collection read-only. This means that you can add / remove parts, but you cannot change what the property points to.

0
source

Source: https://habr.com/ru/post/1333214/


All Articles