简体   繁体   中英

What's the type of “Method” itself in C#?

I heard that C# is a pure object-oriented programming language, because all of things are derived from System.Object .

So I doubt what's the type of the Method itself. I tried with StackTrace and StackFrame to get type of method, but I just got the name of class, which methods are declared.

Of course, I tried this:

var func = new TestClass().TestMethod; //TestMethod is a method of TestClass

however I got only this error.

CS0815    Cannot assign method group to an implicitly-typed variable 

Methods can be assigned to Func<..., TResult> typed variables. I tested with Func and method that returns string, then I got this:

// Result of
// Func<string> func = new TestClass().TestMethod;
// func.GetType();
Instance TestClass -> TestMethod Type : System.Func`1[System.String]

But it's a typename of Func<string> . So, What's the real typename of methods?

Edit Feb 10, 2018 01:46 AM, KST (GMT +09:00)

Alright, let me summarize. Function itself have no type, also it is not an object, and Delegate / Func<> is a just feature of .NET Framework to handle functions as first class citizen( but it is not an FCC ). is it correct?

Methods don't have a type . Methods are what they are - just "callable functions" on an object or class. The fact that you can assign them to Delegates or Func with same signature is just a feature of the framework, but the method on itself doesn't have any "type".

The closest thing to a type could be the return value of the method (if it has one).

A method is not an object therefore it has no type.

A method in C# is the object oriented way of attaching functions to classes, but they are not first class citizens as functions are in JavaScript.

That's why you cannot assign a method to a variable.

You can't do this:

var func = Int32.Parse;

But instead you can do something like this:

Func<string, int> func2 = Int32.Parse;

In the second example despite the fact that it looks like you are actually assigning a method to a variable the reality is that the compiler creates a new instance of a Func<string, int> which is a delegate (which ultimately derives from System.Object ) that happens to actually hold a type-safe reference to the method itself.

That delegate IS an object and can be assigned and passed as parameters to other methods, but is not the method itself but a wrapper.

All things are derived from System.Object, but not everything is a thing.

Object.GetType() explains:

"Because System.Object is the base class for all types in the .NET Framework type system, the GetType method can be used to return Type objects that represent all .NET Framework types. The .NET Framework recognizes the following five categories of types:

Classes, which are derived from System.Object,

Value types, which are derived from System.ValueType.

Interfaces, which are derived from System.Object starting with the .NET Framework 2.0.

Enumerations, which are derived from System.Enum.

Delegates, which are derived from System.MulticastDelegate."

An instance method is not one of those, it has no meaning apart from its class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM