c# - Initialize my class with array syntax -
is there anyway initialize class array or dictionary, example
private class { private list<int> _evenlist; private list<int> _oddlist; ... }
and say
a = new {1, 4, 67, 2, 4, 7, 56};
and in constructor fill _evenlist , _oddlist values.
to utilize collection initializer, class has to:
implementienumerable
implement appropriate add
methods for example:
class : ienumerable { private list<int> _evenlist = new list<int>(); private list<int> _oddlist = new list<int>(); public void add(int value) { list<int> list = (value & 1) == 0 ? _evenlist : _oddlist; list.add(value); } // explicit interface implementation discourage calling it. // alternatively, implement (and ienumerable<int>) // in fashion. ienumerator ienumerable.getenumerator() { throw new notimplementedexception("not enumerable..."); } }
c# .net class oop constructor
No comments:
Post a Comment