c# cast problem

Lloyd Sheen 1,351 Reputation points

Really boggled.

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
string[] ss = (string)value.Split("|");

This throws a compile error. Now since I am casting the field "value" I cannot see why the Split method is not useable. I can simply case value to another field defined as string and then use that field in the Split method. Why can I not do the whole thing in one statement?

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,192 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 26,271 Reputation points Microsoft Vendor

    Hi @Lloyd Sheen ,
    (string) is converted last.
    So string[] ss = (string)value.Split("|");, What actually happens is the following steps.

    string[] a = value.Split("|");  
    string[] ss = (string)a;  

    It is recommended to use your step-by-step code, or try string[] ss = value.ToString().Split("|");.

    Best Regards.
    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Muhammed Şeker 76 Reputation points

    can you try this?

    string x=value.ToString();
    string[] ss = value.Split("|");

    0 comments No comments