Chương trình minh họa cách sử dụng chỉ định quyền hạn truy cập (Access Specifiers) trong C-Sharp. (Program to Illustrate the Use of Access Specifiers in C-Sharp.)

Đây là một chương trình dùng để minh họa cách sử dụng các chỉ định quyền hạn truy cập trong tiếng anh gọi là Access Specifiers trong ngôn ngữ lập trình C-Sharp bào gồm: private, protected, internal, public, protected internal.

Program to Illustrate the Use of Access Specifiers in C-Sharp


C-Sharp Basic Example:
--------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpExample
{
    class accessspecifier
    {

        static void Main(string[] args)
        {
            two B = new two();
            B.show();
        }

        class one
        {
            private int a;
            protected int b;
            internal int c;
            public int d;
            protected internal int e;
        }


        class two : one
        {

            public void show()
            {
                Console.WriteLine("Values are : ");

                //a=10; //it's private.
                b = 20; //it's protected
                c = 30; //it's internal
                d = 40; //it's public
                e = 50; //it's protected internal


                //Console.WriteLine(a); // Error x is not accessible
                Console.WriteLine("Priveate a is not accessible: ");
                Console.WriteLine("Protected: " + b);
                Console.WriteLine("Internal: " + c);
                Console.WriteLine("Public: " + d);
                Console.WriteLine("Protected Internal: " + e);
                Console.ReadLine();
            }


        }
    }
}
--------------------------













No comments:

Post a Comment