c# cast problem

Lloyd Sheen 1,351 Reputation points
2022-09-04T16:08:12.3+00:00

Really boggled.

Code
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?

C#
C#
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
    2022-09-05T01:34:07.58+00:00

    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
    2022-09-05T10:34:43.867+00:00

    can you try this?

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

    0 comments No comments