Search code examples
c#class-design

Should a c# class generate instances of itself?


I have a class that defines a CallRate type. I need to add the ability to create multiple instances of my class by reading the data from a file.

I added a static method to my class CallRate that returns a List<CallRate>. Is it ok for a class to generate new instances of itself by calling one of its own constructors? It works, I just wonder if it's the proper thing to do.

List<CallRates> cr = CallRates.ProcessCallsFile(file);

Solution

  • It is perfectly fine to get object(s) of its own from the static method.

    e.g.

    One of the dot net libraries does the same thing as you did,

    XmlReadrer reader = XmlReader.Create(filepathString);