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
This is because Join is a static method on string. static methods are always called using the class name, not the name of a variable for an instance of that class.
You can look at MSDN's documentation for the String class for a list of all available methods. Static methods have an orange "S" icon next to them, while instance methods do not, e.g.:
When thinking about the difference between static and instance methods, think about whether the functionality of that method depends on a particular instance of the class.
If the answer is yes, then it's likely to be implemented as an instance method; and if not, it's likely to be a static method.
For example, the Split
method separates a specific instance of the String
class (the string from which it is called) into an array.
In contrast, the Join
method is like a utility method, which can be called upon to combine an array separated by a string -- without first creating an instance of the String
class in order to call it.
Join is a static method on the String class, whereas Split is an instance method. To find out which is which, right click on String and Go To Definintion. This will show you the definition of the class (with method declarations, etc.)
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<string> 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
)
This is the perfect Answer.. It Works well..
A static member function does not belong to a specific instance of a class but rather to the class type itself. As a result, when you are attempting to access it for a specific instance, the compiler is not allowing this.
To call the function correctly, you would have to write
classname.methodname(parameter)
rather than Classname cl=new Classname(); cl.methodname(parameter);
or remove the static modified.
For more Please Refer this.. http://www.codeguru.com/forum/showthread.php?t=438550
I would assume its because the method is not directly related to the String
reference in any way and in order to avoid any confusion they made it a static method of String
instead, whereas Split is directly related as you're splitting that particular String
instance.