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#.

 

Share your thought