Typeof (C # reference)
Used to obtain the typeSystem. TypeObject.TypeofThe expression is in the following format:
System. Type type = typeof (INT );
Remarks
To obtain the runtime type of the expression, you can use the. NET Framework method.GetType, As shown below:
Int I = 0; system. Type type = I. GetType ();
TypeofOperators can also be used for public generic types. There must be a proper number of commas in the specification with more than one type parameter. Cannot be reloadedTypeofOperator.
Example // cs_operator_typeof.csusing system; using system. reflection; public class sampleclass {public int samplemember; Public void samplemethod () {}static void main () {type T = typeof (sampleclass); // Alternatively, you cocould use // sampleclass OBJ = new sampleclass (); // type T = obj. getType (); console. writeline ("Methods:"); methodinfo [] methodinfo = T. getmethods (); foreach (methodinfo minfo in methodinfo) cons Ole. writeline (minfo. tostring (); console. writeline ("members:"); memberinfo [] memberinfo = T. getmembers (); foreach (memberinfo minfo in memberinfo) console. writeline (minfo. tostring () ;}} Output Methods: void samplemethod () system. type GetType () system. string tostring () Boolean equals (system. object) int32 gethashcode () members: void samplemethod () system. type GetType () system. string tostring () Boolean equals (system. OBJ ECT) int32 gethashcode () void. ctor () int32 samplemember this example uses the GetType method to determine the type of the result to contain a numerical value. This depends on the storage requirements of result numbers. // Cs_operator_typeof2.csusing system; Class gettypetest {static void main () {int radius = 3; console. writeline ("Area = {0}", radius * Math. pi); console. writeline ("the type is {0}", (radius * Math. pi ). getType () ;}} Output Area = 28.2743338823081the type is system. double