Access Specifier Examples C#

In this chapter you will learn:
  • How to implement access specifier in a program?
  • How to use get set modifier in C# programming?
Qu 1: Write a program to demonstrate private access specifier.
Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Example1
{
    class Program
    {
        private void add()
        {
            int num1, num2, result;
            Console.Write("Enter a number:\t");
            num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("\nEnter second number:\t");
            num2 = Convert.ToInt32(Console.ReadLine());

            result = num1 + num2;
            Console.WriteLine("{0} + {1} = {2}", num1, num2, result);
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.add(); //It is valid, because private add() is in same class
            Console.ReadLine();
        }
    }
}

Output

Enter a number: 6
Enter second number:     8
6 + 8 = 14 __
 
Qu2: Write a program to explain get set accessor.
Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Example2
{
    class input
    {
        private static int num1, num2, result;
        public void add()
        {
            result = num1 + num2;
            Console.WriteLine("\n\nAdd = {0}", result);
            Console.ReadLine();
        }

        // Creating property for storing value in num1
        public int Number1
        {
            get
            {
                return num1;
            }
            set
            {
                num1 = value;
            }
        }

        // Creating property for storing value in num2
        public int Number2
        {
            get
            {
                return num2;
            }
            set
            {
                num2 = value;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            input inp = new input();
            Console.Write("Enter number 1st:\t");
            inp.Number1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter number 2nd:\t");
            inp.Number2 = Convert.ToInt32(Console.ReadLine());

            inp.add();
        }
    }
}

Output

Enter number 1st:      56
Enter number 2nd:     87Add = 143
__

Summary

In this chapter you learned how to implement various types of access specifier and get set modifier in C# programming. In next chapter, some programming questions are given. You must do the exercises in order to improve your programming skills.

More Articles

 

Share your thought



PLEASE DISABLE ADBLOCKER AND SUPPORT US!

Please support us by enabling ads on this page. Refresh

YOU DON'T LIKE ADS, WE ALSO DON'T LIKE ADS!
 
But we have to show ads on our site to keep it free and updated. We have to pay huge server costs, domain costs, CDN Costs, Developer Costs, Electricity and Internet Bill. Your little contribution will encourage us to regularly update this site.