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.