public class RegistrationManager
{
public List<object> RegisteredObjects;
public bool TryRegisterObject(object o)
{
// ...
// Add or not to Registered
// ...
}
}
I want that RegisterObjects
be accessible from outside of the class, but also that the only way to populate the RegisterObjects
list is through TryRegisterObject(object o)
.
Is this possible ?
Essentially, you're setting a Tag's name to the first value in tagList and adding it to the collection, then you're changing that same Tag's name to the second value in tagList and adding it again to the collection. Your collection of Tags contains several references to the same Tag object!
C# List access elements. Elements of a list can be accessed using the index notation [] . The index is zero-based.
C# - List<T> The List<T> is a collection of strongly typed objects that can be accessed by index and having methods for sorting, searching, and modifying list. It is the generic version of the ArrayList that comes under System.Collections.Generic namespace.
Add(new string[] { "Id", "Name", "Age" }); for(int i=0; i<list. Count; i++) for(int j=0; j<list[i]. Count(); j++) Console. WriteLine(list[i][j]); // simpler with foreach foreach(var l in list) foreach(string s in l) Console.
I would hide it under ReadonlyCollection. In this case client won't be able to add elements via casting to IList for example. It totaly depends on how secure you want to be (in simplest scenario exposing IEnumerable will be pretty enough).
public class RegistrationManager
{
private List<object> _registeredObjects;
ReadOnlyCollection<object> _readOnlyRegisteredObjects;
public RegistrationManager()
{
_registeredObjects=new List<object>();
_readOnlyRegisteredObjects=new ReadOnlyCollection<object>(_registeredObjects);
}
public IEnumerable<object> RegisteredObjects
{
get { return _readOnlyRegisteredObjects; }
}
public bool TryRegisterObject(object o)
{
// ...
// Add or not to Registered
// ...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With