Implementing methods for casting and attempting transformations in C #

Source: Internet
Author: User
This article is mainly for you to introduce in detail the C # cast and try to convert the method, with a certain reference value, interested in small partners can refer to

The example of this article for you to share the C # cast and try to convert the method for your reference, the specific content is as follows

Convert an object type of string[] type to a string[] type:


public string Objecttostring (object ob) {  string str = string. Empty;  if (ob is string[])  {    string[] strlist = (string[]) ob;   }  return str;}

Use is to determine if OB is string[] type.

To convert a string type to a DateTime type:


public datetime stringtodatetime (String str) {  datetime datetime = new DateTime ();  if (Datetime.tryparse (str, out datetime))  {    return datetime;  }  return dateTime;}

Attention:

Use Datetime.tryparse (); When the conversion is judged, if true, the cast result is passed into DateTime, and if False is returned, it cannot be cast.

You can also use Convert.todatetime (); Cast, but it is not possible to determine whether a cast can be made, and an exception is reported when the conversion cannot be cast.

Related Article

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.