Access modifiers in C#



Access modifiers:-
Ø  Also called as access specifiers
Ø  These are the keywords, used to implement security in oop programs.
Access modifiers in c#:-
   1) Private
   2) Protected
   3) Public
   4) Internal
   5) Protected internal

1) Private:-
Ø  These members are accessible within the same class only; not in other classes.
2) Protected:-
Ø  These members are accessible within the same class; and also accessible in its child classes too; but not in other classes.
using System;

namespace AccessModifiersDemo
{
    class User
    {
        //data member
        private string UserID;
        protected DateTime LastLoginDate;
        internal int NoOfDownloads;

        protected void Upload()
        {
            //some code
        }

        public void ClearHistory()
        {
            //some code
        }
    }

    class PaidUser : User
    {

    }

    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
3) Internal:-
Ø  These members are accessible within the same class; and also in its child classes; and also in other classes present within the same project; but not in other projects.
4) Protected internal:-
Ø  Protected internal = protected + internal
Ø  These members are accessible anywhere within the same project; and also its child classes present within the other projects
5) Public:-
Ø  Accessible anywhere.


Access modifiers:-
Ø  Also called as access specifiers
Ø  These are the keywords, used to implement security in oop programs.
Access modifiers in c#:-
   1) Private
   2) Protected
   3) Public
   4) Internal
   5) Protected internal

1) Private:-
Ø  These members are accessible within the same class only; not in other classes.
2) Protected:-
Ø  These members are accessible within the same class; and also accessible in its child classes too; but not in other classes.
using System;

namespace AccessModifiersDemo
{
    class User
    {
        //data member
        private string UserID;
        protected DateTime LastLoginDate;
        internal int NoOfDownloads;

        protected void Upload()
        {
            //some code
        }

        public void ClearHistory()
        {
            //some code
        }
    }

    class PaidUser : User
    {

    }

    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
3) Internal:-
Ø  These members are accessible within the same class; and also in its child classes; and also in other classes present within the same project; but not in other projects.
4) Protected internal:-
Ø  Protected internal = protected + internal
Ø  These members are accessible anywhere within the same project; and also its child classes present within the other projects
5) Public:-
Ø  Accessible anywhere.

No comments:

Post a Comment