Public Access Modifier in C# with Example

C#Public access modifier is one of the 5 access modifiers in C#.  Public access modifier is only used when you want to make any type member to be globally accessible within the containing type as well as outside of the containing type.  An example of Public access modifier in C# is given below.

 

using System;



namespace Hello_World
{

    class Customer
    {
        public string name;

        public void PrintName()
        {
            Console.WriteLine(name);
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Customer cust = new Customer();
            cust.name = "Robert Marks"; //Assigning Public Type Member
            cust.PrintName(); //Calling Public Method
        }
    }
}

 Output:

Robert Marks
Press any key to continue . . .

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •