2

In C Programming,

void foo()
{
}
void main()
{
  printf("%p",foo);
}

will print the address of foo function. Please let me know if there is a way in C# to achieve the same.

Srikanth P Vasist
  • 1,327
  • 2
  • 14
  • 26

2 Answers2

4

C# is a high-level language. A method does not need to have an "address" -- this is an implementation detail left to the runtime.

However, if you need to interface with C code that requires a method address (for example, to provide a callback to a Windows API method), you can

  • create a delegate and
  • retrieve a function pointer to that delegate.

Example:

static void foo()
{
}

static void Main(string[] args)
{
    Delegate fooDelegate = new Action(foo);

    IntPtr p = Marshal.GetFunctionPointerForDelegate(fooDelegate);

    Console.WriteLine(p);
}

Note, though, that you usually won't need this. Even for the aforementioned example -- passing a callback to a Windows API function -- there are more elegant solutions.


Update 2021: C# 9 now supports function pointers in unsafe code:

static void Foo() { }

static void Main(string[] args)
{
    unsafe
    {
        delegate*<void> ptrFoo = &Foo;
        Console.WriteLine((long)ptrFoo);
    }
}
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • 1
    This won't return address of "new Action" but delegate which stores internally address to "new Action". And "p" pointer will be pointer to delegate to new action - so it will be calls away from searched address. – Adas Lesniak Jun 16 '19 at 20:05
  • I think this is what @AdasLesniak is saying, but just to be clear, you can't actually get the address of a class method this way. I'm not sure there is any way to do so, or that you could do anything useful with that address even if you could. If you want to call C# methods from unmanaged C++, COM interop is the best way and takes care of all this for you. – Emperor Eto Mar 26 '21 at 14:13
  • Also of course methods have addresses. COM interop wouldn't work otherwise. It's just virtually impossible to obtain them directly. You may be right that it's implementation specific, but I'd like to see an implementation that doesn't store code in memory. – Emperor Eto Mar 26 '21 at 15:07
  • 1
    @PeterMoore: I haven't checked the spec whether that's allowed, but I imagine that a compiler or JITter could first inline and then completely remove a private or internal method. I that case, the method would *really* not have an address. – Heinzi Mar 26 '21 at 18:16
  • Wow you just blew my mind - a method itself getting GC'd sometime after being invoked? I guess it's possible - but sounds insane. Gives me a headache thinking about it. What I do know for sure is that if you get an IUnknown for a managed object, those methods are JITted and exist in RAM as native code, otherwise C/C++ wouldn't be able to use the interface. So I'm just saying it IS theoretically possible to get the physical address but I think we all agree it's at best insanely difficult and pointless. – Emperor Eto Mar 26 '21 at 18:58
0

In C# methods don't provide address. In C method addresses are available to create pointers to functions that can be used to alternatively call functions by passing them to functions that accept these pointers. In C# you could achieve the same using delegates that are typesafe and these delegates can contain multiple functions too...

Bharat
  • 152
  • 1
  • 9