I am wrapping a native C++ class, which has the following methods:
class Native { public: class Local { std::string m_Str; int m_Int; }; typedef std::vector<Local> LocalVec; typedef LocalVec::iterator LocalIter; LocalIter BeginLocals(); LocalIter EndLocals(); private: LocalVec m_Locals; };
1) What is the ‘.NET way’ of representing this same kind of interface? A single method returning an array<>? Does the array<> generic have iterators, so that I could implement BeginLocals() and EndLocals()?
2) Should Local be declared as a value struct in the .NET wrapper?
I’d really like to represent the wrapped class with a .NET flavor, but I’m very new to the managed world – and this type of information is frustrating to google for…
Iterators aren’t exactly translatable to ‘the .net way’, but they are roughly replaced by IEnumerable < T > and IEnumerator < T >.
Rather than
you would see (in c#)
Or more explicitly (without hiding the IEnumerator behind the foreach syntax sugar):
As you can see, foreach just hides the .net enumerator for you.
So really, the ‘.net way’ would be to simply allow people to create List< Local > items for themselves. If you do want to control iteration or make the collection a bit more custom, have your collection implement the IEnumerable< T > and/or ICollection< T > interfaces as well.
A near direct translation to c# would be pretty much what you assumed:
Then a user would be able to