Public Access Specifiers (C#)

In this chapter you will learn:
  • What is public access specifier?
  • What is the boundary of public access specifier?
  • How to use public access specifier in C# programming?

The class member, that is defined as a public can be accessed by other class members that are initialized outside the class. A public member can be accessed from anywhere even outside the namespace.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Public_Access_Specifiers
{
    class access
    {
        // String Variable declared as public
        public string name;
        // Public method
        public void print()
        {
            Console.WriteLine("\nMy name is " + name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            access ac = new access();
            Console.Write("Enter your name:\t");
            // Accepting value in public variable that is outside the class
            ac.name = Console.ReadLine();
            ac.print();

            Console.ReadLine();
        }
    }
}

 

Output

Enter your name:     Steven Clark
My name is Steven Clark
__

Summary

In this chapter you learned about what public access specifier is and what the boundary of public access specifier is. You also learned how to use public access specifier in C# programming. In next chapter you will learn about Private Access Specifier in C#.

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.