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 6327427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:16:41+00:00 2026-05-24T17:16:41+00:00

In python, an instance method self points to the class instance, just like this

  • 0

In python, an instance method self points to the class instance, just like this in C#.
In python, a class method self points to the class. Is there a C# equivalent?

This can be useful, example:

Python example:

class A:
  values = [1,2]

  @classmethod
  def Foo(self):
    print "Foo called in class: ", self, self.values

  @staticmethod
  def Bar():
    print "Same for all classes - there is no self"

class B(A):
  # other code specific to class B
  values = [1,2,3]
  pass

class C(A):
  # other code specific to class C
  values = [1,2,3,4,5]
  pass

A.Foo()
A.Bar()
B.Foo()
B.Bar()
C.Foo()
C.Bar()

Results in:

Foo called in class:  __main__.A [1, 2]
Same for all classes - there is no self
Foo called in class:  __main__.B [1, 2, 3]
Same for all classes - there is no self
Foo called in class:  __main__.C [1, 2, 3, 4, 5]
Same for all classes - there is no self

This can be a great tool so that common code in a class context (without an instance) can provide customised behaviour that is defined by the subclass (without requiring an instance of the subclass).

It seems to me that C# static methods are exactly like pythons static methods, in that there is no access to which class was actually used to invoke the method.

But is there a way to do class methods in C#??
Or at least determine which class invoked a method, for example:

public class A
{
  public static List<int> values;

  public static Foo()
  {
    Console.WriteLine("How can I figure out which class called this method?");
  }
}

public class B : A
{
}

public class C : A
{
}

public class Program
{
  public static void Main()
  {
    A.Foo();
    B.Foo();
    C.Foo();
  }
}
  • 1 1 Answer
  • 0 Views
  • 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-24T17:16:41+00:00Added an answer on May 24, 2026 at 5:16 pm

    There is no way to do this using regular static methods. Possible alternatives include:

    1) Virtual, overridden instance methods:

    public class A
    {
        public virtual void Foo()
        {
            Console.WriteLine("Called from A");
        }
    }
    
    public class B : A
    {
        public override void Foo()
        {
            Console.WriteLine("Called from B");
        }
    }
    

    2) Extension methods:

    public class A
    {
    }
    
    public class B : A
    {
    }
    
    public static class Extensions
    {
        /// Allows you to do:
        /// var whoop = new B();
        /// whoop.Foo();
        public static void Foo<T>(this T thing) where T : A
        {
            Console.WriteLine("Called from " + thing.GetType().Name);
        }
    }
    

    3) Assuming A and B have a default constructor:

    public static class Cached<T> where T : class, new()
    {
        private static T _cachedInstance;
    
        public static T Instance
        {
            get { return _cachedInstance ?? (_cachedInstance = new T()); }
        }
    }
    
    public static class Extensions
    {
        public static void Example()
        {
            Cached<B>.Instance.Foo();
        }
    
        public static void Foo<T>(this T thing) where T : A, new()
        {
            Console.WriteLine("Called from " + typeof(T).Name);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an instance of some class in Python, it would be useful to be
In Python, I want to make selected instance attributes of a class be readonly
for instance in python it is possible to assign a method to a variable:
In the book Python in a Nutshell (2nd Edition) there is an example which
How can one check if a variable is an instance method or not? I'm
From what I've read, any changes to the environment variables in a Python instance
Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
Python works on multiple platforms and can be used for desktop and web applications,
Python uses the reference count method to handle object life time. So an object
Python gives us the ability to create 'private' methods and variables within a class

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.