I think you are confusing the concepts of static function, constructor and factory method.
Static function
Definition
This is a method that does not have access (and is not associated) with an instance of this class.
Example
public class BusinessHelper { public static List<Business> ListBusinesses() { List<Business> businesses = new List<Business>(); businesses.Add(new Business("1", "Business Name 1")); businesses.Add(new Business("2", "Business Name 2")); return businesses; } }
Using
Call a static method with the name of the class, not an instance of the class.
List<Business> businesses = BusinessHelper.ListBusinesses();
Constructor . This is the method that instantiates this class. It has no return value and is called when the object is created.
Example
public class BusinessList { public List<Business> TheList; public BusinessList() { TheList = new List<Business>(); TheList.Add(new Business("1", "Business Name 1")); TheList.Add(new Business("2", "Business Name 2")); } }
Using
Create a new instance of the object.
BusinessList myBusinessList = new BusinessList(); businesses = myBusinessList.TheList;
Factory Method
Definition
This is a method that creates an instance of an object, somehow creates it and returns a link to it.
Example
public class BusinessList { public List<Business> TheList; public static BusinessList BusinessListWithTwoCompanies() { BusinessList instance = new BusinessList(); businesses = new List<Business>(); businesses.Add(new Business("1", "Business Name 1")); businesses.Add(new Business("2", "Business Name 2")); return instance; } }
Using
Call the factory method instead of creating a new object.
BusinessList myBusinessList = BusinessList.BusinessListWithTwoCompanies(); businesses = myBusinessList.TheList;
Two things to note additionally:
You declare the businesses field, but proceed to create another variable named businesses in your ListOfBusinesses() method and return it. Nothing will happen to the businesses field. Be careful with variable coverage.
You cannot have a member (field, property, or method) with the same name as the class. This is reserved for a constructor that does not have a return type (see above). This is why you get a compiler error.