Public Access Specifier
Public Access Specifier एक Class को अपने Member Variables और Member Functions को अन्य Functions और Objects के लिए Expose करने की अनुमति देता है। किसी भी Public Member को Class के बाहर से Access किया जा सकता है।
Example
using System; namespace Demo { class Rectangle { public double length; public double width; public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } } //end class Rectangle class ExecuteRectangle { static void Main(string[] args) { Rectangle r = new Rectangle(); r.length = 7; r.width = 10; r.Display(); Console.ReadLine(); } } }Output
Length: 7 Width: 10 Area: 70
Protected Access Specifier
Protected Access Specifier एक Child Class को अपने Base Class के Member Variables और Member Functions को Access करने की अनुमति देता है।
आइए Protected Access Modifier का एक Example देखते हैं, Protected Members को Access करते हुए।
Example
using System; namespace MySpecifiers { class Demo { protected string name = "Website"; protected void Display(string str) { Console.WriteLine("Tabs: " + str); } } class Test : Demo { static void Main(string[] args) { Test t = new Test(); Console.WriteLine("Details: " + t.name); t.Display("Product"); t.Display("Services"); t.Display("Tools"); t.Display("Plugins"); } } }Output
Details: Website Tabs: Product Tabs: Services Tabs: Tools Tabs: Plugins
Private Access Specifier
Private Access Specifier एक Class को अपने Member Variables और Member Functions को अन्य Functions और Objects से Hide करने की अनुमति देता है। केवल उसी Class के Functions इसके Private Members को Access कर सकते हैं। यहां तक कि Class का Instance भी अपने Private Members को Access नहीं कर सकता।
Example
using System; namespace Demo { class Rectangle { // Member Variables private double length; private double width; public void Acceptdetails() { length = 10; width = 15; } public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); } }//end class Rectangle class ExecuteRectangle { static void Main(string[] args) { Rectangle r = new Rectangle(); r.Acceptdetails(); r.Display(); Console.ReadLine(); } } }Output
Length: 10 Width: 15 Area: 150