Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
IN what situation i should use static function ?
I understood the difference between public static and public functions.
it is
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).
but where i should use static?
I can access a function irrespective to whether it is static or public static.
Then what is the point of declaring it as public static ?
Posted
Comments
Marcin Kozub 5-Dec-14 3:02am    
This topic came up frequently on the internet. There are articles on CodeProject too:
http://www.codeproject.com/Articles/15269/Static-Keyword-Demystified
http://www.codeproject.com/Tips/223095/Simple-Singleton-Pattern-in-Csharp
http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp
http://stackoverflow.com/questions/205689/class-with-single-method-best-approach#206481

There is no need to start another discussion :)
Marcin Kozub 5-Dec-14 3:07am    
And you're wrong. Static properties can be used in both cases by static method of that class and instance method of that class. But using Instance properties is allowed only in instance methods.

public class SomeClass
{
public string SomeNonStaticProperty { get; set; }
public static string SomeStaticProperty { get; set; }

public void NonStaticMethod()
{
SomeStaticProperty = "Test";
SomeNonStaticProperty = "Test";
}

public static void StaticMethod()
{
SomeStaticProperty = "Test";
SomeNonStaticProperty = "Test"; // This won't compile
}
}

static mymethod() is compiled as internal which means you can only access it within the DLL which contains it.

public static mymethod() is public to all DLL's which reference it.

So if you the general rule is define things internal unless you know you will be using it in other projects.

As for when to use static methods generally when the method does not change state of something in the class or uses state of the containing class, or for computation functions where you pass parameters and get a result.
 
Share this answer
 
Check my Article for Static Class and Method Details.
Basic C# OOP Concept[^]

Now lets focus on your question
So you are asking the differance between static method and Public Static Method

As you know if we declare class or static or method as static we need to create object for to access that methods.
We can directly access the static method usign there class
example Yourclass.YourStaticMethod();

if you declare only static method by default it will be as private so outside class cannot access that method.
If you declare the static method as public outside class can access that method.

see the sample console program below for static method and public static method.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    public class YourstaticClass
    {
        static void welcomewithprivate()
        {
            Console.WriteLine("iam in Private Static Clss Method");
        }
        public static void WelcomewithPublic()
        {
            Console.WriteLine("iam in Private Static Clss Method");
        }
    }
    class Program
    {
       
        static void Main(string[] args)
        {
            //This line will be as error due to private static method can not be accessed in another class
            //YourstaticClass.welcomewithprivate(); 
            // Public Static method
            YourstaticClass.WelcomewithPublic();
            

        }
    }
}
 
Share this answer
 
Quote:
A property declared as static can not be accessed with an instantiated class object
That's not true, a static property can be accessed from everywhere. It is true the opposite, a static method cannot directly access instance properties.

Quote:
but where i should use static?
You should use static method when the implemented functionality does not depend on particular instance state.
 
Share this answer
 
1.The static keyword is for accessing class members that belongs to the class itself rather than to a specific object.

For example you can use static properties to stores data that are used/shared in common by all objects of a specific class, like application settings.

And you could use static methods for methods that are shared in common by all objects and do not depends on the objects non static members. This static methods could provide utility features like string.Format() is doing in the case of system string class.

2.Regarding your 2nd question about public static , you know that public access is the most permissive access level and this is valid also for static members.

So if you need that a static property or method to be accessible form other classes that do not inherit the parent class of your static members, you have to make your static members public, otherwise you could use internal, protected or even private (in the case when your static members is used only internally in your class).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900