C
C#2y ago
Magix

Make a new type that essentially acts like another

In my project, I've found that Im making a lot of structs/classes that are essentially containers for other stuff. Example:
public struct ArrayInstanceData
{
public string Name;
public bool IsDictionary;
public Dictionary<string, DataValue> Data;
public DataValue Value => Data["value"]; // non dictionary
public ArrayInstanceData()
{
Name = "NULL"; // unset default
IsDictionary = true; // default
Data = new Dictionary<string, DataValue>();
}
}
public struct ArrayContainer
{
public ArrayInstanceData[] Data;
}
public struct ArrayInstanceData
{
public string Name;
public bool IsDictionary;
public Dictionary<string, DataValue> Data;
public DataValue Value => Data["value"]; // non dictionary
public ArrayInstanceData()
{
Name = "NULL"; // unset default
IsDictionary = true; // default
Data = new Dictionary<string, DataValue>();
}
}
public struct ArrayContainer
{
public ArrayInstanceData[] Data;
}
How can I make a type that essentially acts like that ArrayInstanceData[] Data?
5 Replies
Pobiega
Pobiega2y ago
So you want to make a class that behaves like a collection?
Magix
Magix2y ago
yeah
Pobiega
Pobiega2y ago
Alright, well you'll want to implement this[int index] so the collection can be indexed use Collection<T>
Magix
Magix2y ago
how would I implement this for a dictionary type? nvm i realize i dont need to