what word can i write to access static function inside class? like self:: in php?
-
Your question is unclear. Please give an example of what you're trying to do, in terms of what you're trying to call and from where.Jon Skeet– Jon Skeet2011-03-07 10:41:18 +00:00Commented Mar 7, 2011 at 10:41
-
Is the status function defined in the same class or a different class? What have you tried so far?ChrisF– ChrisF2011-03-07 10:41:48 +00:00Commented Mar 7, 2011 at 10:41
Add a comment
|
6 Answers
You just use the type name:
static class Test
{
public static string GetSomething()
{
return "Something";
}
}
string s = Test.GetSomething();
If you're in the class already you just call the method.
3 Comments
Lloyd
What do you mean not pretty? Drop the type name from the call if inside the class, all you need to do is call GetSomething() etc.
kusanagi
i want show that calling function belongs to current class where it called
Lloyd
Without an explicit reference to another class then it is assumed that it belongs to the current class or a base class (and you can show inherited calls through the use of the base keyword).
Simply use StaticMethodName(...) (when inside the class where the static method is defined) or ClassName.StaticMethodName(...).
1 Comment
Jaroslav Jandek
@jeroenh: I fixed the typo ~6 minutes ago :)
There is no such keyword in C#. You need to use the class name, e.g.
MyClass.StaticMember
1 Comment
Hans Kesting
From inside the class itself, you don't need to specify the classname for a static method (it's not an error though).