Can a static method return an instance of the class its defined in?

Posted on 2005-04-19
Last Modified: 2010-04-16
If i've got a class, can i have a static method in that class that returns an instace of the class itself?

I'm trying to build a static method on the class that can act as a sort of database lookup - i.e. people could call
Subscription.returnExistingSubscriptionFromDatabase("test006") - and get a Subscription object with its properties populated from the database.  Can i do this?


private class Subscription()
    private string property1
    private string property2
    // static method taht could be used to return an instance of this class
    public static subscription returnExistingSubscriptionFromDatabase(String uniqueIdentifier)
         // look up database to find the data for property1 and property2
         // return by calling the class constructor
         return subscription(valueFromDatabase1, valueFromDatabase2)

    public subscription(string prop1, string prop2)
        this.property1 = prop1
        this.property2 = prop2

Question by:kenshaw
    1 Comment
    LVL 3

    Accepted Solution

    Yeah, you should have no problem doing that. You have a few syntax errors, but once you get those worked out, you should be fine. In particular some semicolons (;) and the class definition.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now