Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 566011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:55:58+00:00 2026-05-13T12:55:58+00:00

Is there a way to do what classmethod does in Python in C#? That

  • 0

Is there a way to do what classmethod does in Python in C#?

That is, a static function that would get a Type object as an (implicit) parameter according to whichever subclass it’s used from.

An example of what I want, approximately, is

class Base:
    @classmethod
    def get(cls, id):
        print "Would instantiate a new %r with ID %d."%(cls, id)

class Puppy(Base):
    pass

class Kitten(Base):
    pass

p = Puppy.get(1)
k = Kitten.get(1)

the expected output being

Would instantiate a new <class __main__.Puppy at 0x403533ec> with ID 1.
Would instantiate a new <class __main__.Kitten at 0x4035341c> with ID 1.

(same code on codepad here.)

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:55:59+00:00Added an answer on May 13, 2026 at 12:55 pm

    In principle, you could write something like this:

    class Base
    {
        public static T Get<T>(int id)
            where T : Base, new()
        {
            return new T() { ID = id };
        }
    
        public int ID { get; set; }
    }
    

    Then you could write var p = Base.Get<Puppy>(10). Or, if you were feeling masochistic, you could write Puppy.Get<Puppy>(10) or even Kitty.Get<Puppy> 😉 In all cases, you have to pass in the type explicitly, not implicitly.

    Alternatively, this works too:

    class Base<T> where T : Base<T>, new()
    {
        public static T Get(int id)
        {
            return new T() { ID = id };
        }
    
        public int ID { get; set; }
    }
    
    class Puppy : Base<Puppy>
    {
    }
    
    class Kitten : Base<Kitten>
    {
    }
    

    You still need to pass the type back up to the base class, which allows you to write Puppy.Get(10) as expected.

    But still, is there a reason to write it like that when var p = new Puppy(10) is just as concise and more idiomatic? Probably not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 297k
  • Answers 297k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer all is a collection of child elements belonging to a… May 13, 2026 at 7:16 pm
  • Editorial Team
    Editorial Team added an answer You need gnonlin. See http://www.jonobacon.org/2006/12/27/using-gnonlin-with-gstreamer-and-python/ You won't need a gnlcomposition… May 13, 2026 at 7:16 pm
  • Editorial Team
    Editorial Team added an answer Why not just create a new Custom Control that extends… May 13, 2026 at 7:16 pm

Related Questions

PHP classes can use the keyword self in a static context, like this: <?php
I have method in a class that I need to make sure is only
In visual studio, is there a way to step back to see what the
I've been working on a web crawling .NET app in my free time, and
My question is pertaining to the best practice for accessing a child object's parent.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.