C # typeof () instance details

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.