. NET 2.0 generic quiz

Source: Internet
Author: User
When will the. NET 2.0 generic be built (construct )? When does the compiler know generic type parameters? Consider the following quiz:

Quiz 1: Write the followingCodeOutput

   Class  Program  ...  {  Static     Void  Main (  String [] ARGs)  ...  {<Int>A= NewA<Int>(); A. internalcall (12);} }    Class  A  <  T  >    ...  {  Public     Void  Internalcall (t obj) ...  {Test (OBJ );}    Public     Void  Test  <  U  >  (U obj) ...  {Console. writeline ("U");}    Public     Void  Test (  Int  OBJ) ...  {Console. writeline ("Int");}  } 

Which overload will be called? Only from the class, it seems that there is only one choice; but from the caller's perspective, there are two choices ......

Quiz 2: Write the output of the following code

   Class  Program ...  {  Static     Void  Main (  String  [] ARGs)  ... {<Int>A= NewA<Int>(); A. Test (12);}  }    Class  A  <  T >    ...  {  Public     Void  Test (t obj)  ... {Console. writeline ("T");}    Public     Void  Test (  Int  OBJ)  ... {Console. writeline ("Int");}  } 

Well ...... This is the most strange example. When you enter the code, IDE will tell you that there are two identical overload functions. Which one will you call?

Quiz 3: Write the output of the following code

   Class  Program  ...  { Static     Void  Main (  String  [] ARGs)  ... {<Int>A= NewA<Int>(); A. Test (12); A. Test (12.0);}  }    Class  A <  T  >    ...  {  Public     Void  Test  <  U  >  (U obj) ...  {Console. writeline ("U");}    Public     Void  Test (t obj) ...  {Console. writeline ("T");}  } 

After you have made these questions, you can think of the features that can be used to compileProgram?

2005.11.08 from: csdn DOTNET blog Expert Group ninputer

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.