C # Generic: Constraints

Source: Internet
Author: User
See the following routine:

Using system;

Namespace consoleapplication1
{
InterfaceItest
{
Void testmethod ();
}

Class Testtype : Itest
{
// Public testtype (int I ){}
Public void testmethod ()
{
Console . Writeline ("testing ");
}

}


Class Test
{
Public static void Foo <t> (T)
Where t: Itest
{
T. testmethod ();
}
}
Class Class1
{
[ Stathread ]
Static void main (string [] ARGs)
{
Object o = newTesttype ();
Test . Foo (O ); // Compile-Error

}
}
}

Note that the actual type of the O object is testtype, which inherits the itest interface, but still reports an error during compilation. That is to say, the check on whether an object meets constraints is completed during the compilation period and does not involve the type information during the runtime. That is to say, in C #, the generic instantiation check is the same as in C ++, and is completed during the compilation period.

As a result, the null syntax like constraints does not bring any benefits at all, and the advantages of the generic itself are also castrated. I really cannot think of the purpose of doing so.

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.