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", "test3" };
teststring = teststring.Join(" ", testarray);
This fails with the following error message:
Member ‘string.Join(string, params string[])’ cannot be accessed with an instance reference; qualify it with a type name instead.
It does however work if I change to:
teststring = string.Join(" ", testarray);
If I however use the function Split as in:
teststring = teststring.Split(new char[] {' '});
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)
These enforced calling techniques between static / non-static sure is something to get used to.
You are correct on the reason;
String.Joinis static butString.Splitis not.You could look at the MSDN docs.
For example, on the page for
String.Join, there is anSnext to the purple box for each method; this indicates that the method is declared asstatic. Additionally, if you click on a particular overload you’ll see the method declared asstatic. For example,However, for
String.Split, there is noSnext to the purple box for each method. For none of the particular overloads is the method declared asstatic. For example