Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sam HobbsRank #107 · 20.5K reputationPosted Oct 9, 2022, 1:55 AM EDT
Note that static can be a modifier and it can be a directive. Public is an access modifier. When everything is called a keyword it is more difficult to know what everything is.
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 8, 2022, 2:19 AM EDT
The Public access modifier states that any variable that is defined using public can be accessed anywhere
in the class and it will be also available when class is object is created and when do class inheritence means
you can access public declared variables anywhere in the application. When you specify class as public, the class
is visible outside the assembly.
Example of using Public access modifier:
public class Person { // Constructor that takes no arguments: public Person() { Name = "Rajanikant"; } // Constructor that takes one argument: public Person(string name) { Name = name; } // Auto-implemented readonly property: public string Name { get; } // Method that overrides the base class (System.Object) implementation. public override string ToString(){return Name;} }The Static modifier declared variables are globally accessible without creating an instance of the class and it will be accessed class level. If static is used with class then it can not be instanciated. Static items can only access other static items. For example, a static class can only contain static members, e.g. variable, methods etc. if we mark method as static then it can only contain static variables and can only access other static items. Static items share the resources between multiple users. Static cannot be used with indexers, destructors or types other than classes. A static constructor in a non-static class runs only once when the class is instantiated for the first time. A static constructor in a static class runs only once when any of its static members accessed for the first time. Static members are allocated in a high frequency heap area of the memory.
Example of using Static modifier:
using System; namespace StaticClass_Demo { public static class HeightConvertor { public static double InchsToCentimeters(string HeightInInchs) { double inchs = Double.Parse(HeightInInchs); double Centimeters = (inchs* 2.54); return Centimeters; } } public static double CentimetesToInchs(string HeightInCentimeters) { double centimeters = Double.Parse(HeightInCentimeters); double Inchs = (centimeters / 2.54); return Inchs; } }The Void is a type modifier that specifies that the method doesn’t return any value but if we use return keyword in void method it just returns control back from this method and no further code execute in this method, refer below example.
Example of using Void type modifier:
public static void Display(IEnumerable numbers) { if (numbers is null) { return; } Console.WriteLine(string.Join(" ", numbers)); }Vishal YelveRank #178 · 13.6K reputationPosted Oct 9, 2022, 11:26 AM EDT
Public, Static and Void comes under different categories. Public is the access specifier, static is a keyword and void comes under return type. let me explain one by one:
Public: Whenever a method, class or a class member declared with public access specifier. it means that the member can be access from any where. There is no restriction on accessing the public member.
If a class is declared us Public we can access the class wherever we want.
if a member of the class is public then we can access the member from anywhere there wont be any restriction on this but class has it restriction if the class isn’t declared public.
Static keyword is used to mention that the static member value won’t change in anytime). throughout the application cycle static member gives same value with referring same memory location.
if a variable is declared with static keyword and assigned so some value. While executing a memory blocks get assigned to the static variable. We can able to access the static members without any instance. We can access the member from any where in the application it will same value referring to same memory location.
Void keyword is used in the return type. which actually stating that the method with void return type wont return anything (it returns nothing. don’t think nothing as null because null is a value).
Sachin SinghRank #66 · 26.2K reputationPosted Oct 9, 2022, 11:34 AM EDT
100% agree with Sam Hobbs sir.
Abhishek YadavRank #217 · 11.3K reputationPosted Oct 19, 2022, 4:35 AM EDT
Public − This is the get right of entry to specifier that states that the approach may be accesses publically. static − Here, the item isn't always required to get right of entry to static members. void − This states that the approach doesn’t go back any value.
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Oct 17, 2022, 3:54 AM EDT
Here is the difference-
public − This is the access specifier that states that the method can be accessed publically.Whenever a method, class or a class member declared with public access specifier. it means that the member can be access from any where. There is no restriction on accessing the public member.
static − Here, the object is not required to access static members.If a class is declared us Public we can access the class wherever we want.
if a member of the class is public then we can access the member from anywhere there wont be any restriction on this but class has it restriction if the class isn’t declared public.
Static keyword is used to mention that the static member value won’t change in anytime). throughout the application cycle static member gives same value with referring same memory location.
void − This states that the method doesn’t return any value.
if a variable is declared with static keyword and assigned so some value. While executing a memory blocks get assigned to the static variable. We can able to access the static members without any instance. We can access the member from any where in the application it will same value referring to same memory location.
Void keyword is used in the return type. which actually stating that the method with void return type wont return anything (it returns nothing. don’t think nothing as null because null is a value).
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Oct 18, 2022, 4:23 AM EDT
public − This is the access specifier that states that the method can be accesses publically.
static − Here, the object is not required to access static members.
void − This states that the method doesn’t return any value.