How does C # string convert to generic T?

Source: Internet
Author: User

Example:

t ret = (t) convert.changetype (defaultvaule,typeof(t));
How to initialize:
You have a generic class that contains a variable that is defined by the class itself as a type parameter. Because of the constraints on generic objects, you want this variable to be initialized to its default value.
T data = default (t);

Example: "
1  PublicT getconfigs<t> (stringValuestringdefaultvaule)2         {3t ret = (t) convert.changetype (Defaultvaule,typeof(T));4 5             Try6             {7ret = (T) convert.changetype (configurationmanager.appsettings[value. ToString ()],typeof(T));8             }9             CatchTen             { One             } A             returnret; -}
Console.WriteLine(GetType<Int32>( "123" )); Console.WriteLine(GetType<DateTime>( "2010-10-15" ));Other development reading: http://www.cnblogs.com/arxive/p/6179972.html

How does C # string convert to generic T?

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.