Why do I get “cannot be accessed with an instance reference” when using teststring.Join but not teststring.Split? (c#)

前端 未结 6 465
南旧
南旧 2021-01-18 11:44

I\'m learning c# and there is something I do not understand that I\'ve been unable to find any help about online.

string[] = testarray = { \"test1\", \"test2         


        
6条回答
  •  抹茶落季
    2021-01-18 12:16

    You are correct on the reason; String.Join is static but String.Split is not.

    I am no longer getting an error. I assume this has something to do with certain functions of the string class being static and some not, but how can I tell which function is static and which is not? (if this is the reason)

    You could look at the MSDN docs.

    For example, on the page for String.Join, there is an S next to the purple box for each method; this indicates that the method is declared as static. Additionally, if you click on a particular overload you'll see the method declared as static. For example,

    public static string Join(
    string separator,
    IEnumerable values
    )
    

    However, for String.Split, there is no S next to the purple box for each method. For none of the particular overloads is the method declared as static. For example

    public string[] Split(
    params char[] separator
    )
    

提交回复
热议问题