Home > OS >  Why do we use public methods?
Why do we use public methods?

Time:11-30

I have a question regarding public and static methods. I am new to coding C# and I know that public methods can only be accessed using an object. However, static methods can be accessed without an object of the class. But what is the point of using a public method? Why not just always use a static method?

CodePudding user response:

The public and static keywords are unrelated.

You can have a public static method. Public means that the method (or other class member (e.g., a property, a field, a delegate, an enum, an inner class)) is available for use by code that belongs to another class (the alternatives are private, internal and a few combinations).

The static keyword indicates that access to that member does not require an instance of the class. In some ways, you can think of a static member as belonging to the class while a non-static member belongs to an instance of the class. The VB language emphasizes that metaphor, using Shared instead of static to describe static members.

There are great advantages to using non-static members (look up encapsulation - it's a good job interview buzz-word). The object-oriented mind set is that an object represents a data structure (a class or a struct in C#) and the operations that can be done on that data structure. Those operations are the non-static methods of the class.

CodePudding user response:

I think Flydog57's answer is much more complete. In my answer I will add code example in hopes of making it a bit easier to understand.

Public - access modifier which determines from where your code(method/class/etc.) can be accessed. Public does not have any restriction so you can access your code from basically anywhere. On the other hand If a method or property or something else is private you can access it from only within the class it belongs to. There are other access modifiers as well kindly take a look here.

Static - static is a modifier keyword which determines wither your method, property etc. should be accessed using an object of the class or should be accessed directly with the class name.

In below code Add is static but Subtraction is not-static. So, we can only access Subtraction with object. In order to access Add we need to use class name "Math.AddAdd(5, 4)". Note: the output is given in the comment.

class Math
{
    public static int Add(int a, int b)
    {
        return a   b;
    }

    public int Subtraction(int a, int b)
    {
        return a - b;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Math math = new Math();
        Console.WriteLine(math.Subtraction(10,4)); // 6
        Console.WriteLine(math.Add(5, 4)); // Error CS0176  Member 'Math.Add(int, int)' cannot be accessed with an instance reference;
    }
}

Now if we remove the public access modifier. We get the below errors. Because the methods no longer could be accessed from the outside of the class. Take a look here to see the default access modifiers of different types.

class Math
{
    static int Add(int a, int b)
    {
        return a   b;
    }

    int Subtraction(int a, int b)
    {
        return a - b;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Math math = new Math();
        Console.WriteLine(math.Subtraction(5, 4)); // Error CS0122  'Math.Subtraction(int, int)' is inaccessible due to its protection level
        Console.WriteLine(math.Add(5, 4)); // Error CS0122  'Math.Subtraction(int, int)' is inaccessible due to its protection level
    }
}

When do you use them: Both of this have lot of use cases. In the below example I am using static property TotalStudent to count the total number of student(object). While I am using Name property to keep the information's which is unique to that particular object. Also like above Math example c# and many other programming languages have Math static class which lets you do sum, add and other operations.

class Student
{
    public string Name { get; set; }
    public static int TotalStudent { get; set; } = 0;

    public Student(string name)
    {
        Name = name;
        TotalStudent  ;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Student s1 = new Student("Bob");
        Console.WriteLine(s1.Name); // Bob
        Console.WriteLine(Student.TotalStudent); // 1
        Student s2 = new Student("Alex");
        Console.WriteLine(s2.Name); // Alex
        Console.WriteLine(Student.TotalStudent); // 2
    }
}
  • Related