Why can't I access the instance link when using teststring.Join but not teststring.Split? (WITH#)

I am learning C #, and there is something that I don’t understand, that I could not find any help on the Internet.

string[] = testarray = { "test1", "test2", "test3" };
teststring = teststring.Join(" ", testarray);

The following error message failed:

Member 'string.Join (string, params string [])' cannot be accessed with reference to the instance; qualify it instead of the type name.

However, it works if I change to:

teststring = string.Join(" ", testarray);

If I however use the Split function, as in:

teststring = teststring.Split(new char[] {' '});

I no longer receive the error message. I suppose this has something to do with certain functions of the string class, which are static and some are not, but how can I determine which function is static and which is not? (if this is the reason)

/ - , .

+5
6

; String.Join , String.Split .

. , - string, , , , , ? ( )

MSDN.

, String.Join S; , static. , , , static. ,

public static string Join(
string separator,
IEnumerable<string> values
)

String.Split S. , static.

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

MSDN String class . "S" , , :

alt text

, , .
"", , , ; , . , Split String (, ) .
, Join , , , String .

+6

, Join - . , .

+4

- String, Split - . , , String To Definintion. ( ..).

+2

, String - String, Split , re String.

+2

.. .

A static member function does not apply to a specific instance of a class, but refers to the type of the class itself. As a result, when you try to access it for a specific instance, the compiler does not allow this.

To call the function correctly, you need to write

classname.methodname (parameter)

not Classname cl = new Class name (); cl.methodname (parameter);

or remove static changes.

Details ... Details ... http://www.codeguru.com/forum/showthread.php?t=438550

+1
source

All Articles