0

I am new on coding just need find out if it is possible to get methods name and print using console.write. Here I have sample class and I want to grab "myName" so I can use it to print.

using System;

namespace Tests
{
    class Class1
    {
        public void myName()
        {
            Console.Write(myName);
        }
    }
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
SQATube
  • 37
  • 1
  • 4

2 Answers2

3

Simply use the nameof keyword

Console.Write(nameof(myName));
Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
-1

You can get like this for full information:

string method = string.Format("{0}.{1}", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, System.Reflection.MethodBase.GetCurrentMethod().Name);

And return this string. Using call this within the metod.
Or short version just for name method, what you want:

string method =System.Reflection.MethodBase.GetCurrentMethod().Name;
Nejc Galof
  • 2,538
  • 3
  • 31
  • 70
  • Try this method `public void neiiic() { var names = Enumerable.Range(0, 15).Select(i => System.Reflection.MethodBase.GetCurrentMethod().Name + "" + i).ToList(); Console.WriteLine(string.Join(Environment.NewLine, names)); }` – Eser Apr 30 '16 at 23:02
  • Thank you quick replay, How about when I need call outside the method, let say I have another method2 and I want to call method1 name inside mthod2, is it possible? – SQATube May 01 '16 at 02:21