Yes it is possible. Actually, you almost decided it yourself. The requirements for using the collection initializer are similar to this Add
method, which uses two methods (which you have) and that the type implements IEnumerable (which you do not see). So that your code works; use something like:
public class Cat : IEnumerable<KeyValuePair<string,string>> { private Dictionary<string,string> catNameAndType = new Dictionary<string,string>(); public Cat() { } public void Add(string catName, string catType) { catNameAndType.Add(catName,catType); } public IEnumerator<KeyValuePair<string,string>> GetEnumerator() { return catNameAndType.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }
driis source share