This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Friday, February 24, 2023

what is access modifiers in c# and Type of modifiers

what is access modifiers in c# and Type of modifiers


Access modifiers are keywords used to specify the declared accessibility of a member or a type.


what is an access modifier?

What are Access Modifiers? Access modifiers are keywords that can be used to control the visibility of fields, methods, and constructors in a class. The four access modifiers in Java are public, protected, default, and private.


What are the 4 access modifiers in C#?

In C#, there are 4 basic types of access modifiers.

public.

private.

protected.

internal.


1.Public acess modifiers:


The public keyword is an access modifier for types and type members. Public access is the most permissive access level.

using System;  

namespace AccessModifiers  

{  

    class Program  

    {  

        class AccessMod  

        {  

            public int num1;  

        }  

        static void Main(string[] args)  

        {  

            AccessMod ob1 = new AccessMod();  

            //Direct access to public members  

            ob1.num1 = 100;  

            Console.WriteLine("Number one value in main {0}", ob1.num1);  

            Console.ReadLine();  

        }  

    }  

}  


2.private acess modifiers:


Private members are accessible only within the body of the class or the struct in which they are declared.


example of private acess modifyer:


using System;  

namespace AccessModifiers  

{  

    class Program  

    {  

        class AccessMod  

        {  

            public int num1;  

            int num2;  

        }  

        static void Main(string[] args)  

        {  

            AccessMod ob1 = new AccessMod();  

            //Direct access to public members  

            ob1.num1 = 100;  

            //Access to private member is not permitted  

            ob1.num2 = 20;  

            Console.WriteLine("Number one value in main {0}", ob1.num1);  

            Console.ReadLine();  

        }  

    }  

}  


3. protected acess modifiers:


A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.


using System;  

namespace AccessModifiers  

{  

    class Program  

    {  

        class Base  

        {  

            protected int num1;  

        }  

        class Derived : Base  

        {  

            public int num2;  

            static void Main(string[] args)  

            {  

                Base ob1 = new Base();  

                Derived ob2 = new Derived();  

                ob2.num1 = 20;  

                // Access to protected member as it is inherited by the Derived class  

                ob2.num2 = 90;  

                Console.WriteLine("Number2 value {0}", ob2.num2);  

                Console.WriteLine("Number1 value which is protected {0}", ob2.num1);  

                Console.ReadLine();  

            }  

        }  

    }  

}  


4.internal modifiers:


The internal keyword is an access modifier for types and type members. We can declare a class as internal or its member as internal. Internal members are accessible only within files in the same assembly (.dll).



 

No comments:

Post a Comment