Public

What Does Public Mean?

Public, in C#, is a keyword used to declare the accessibility of a type and type member such that the access is not limited. It is one of the access modifiers that provides complete visibility to all types and type members.

Advertisements

C# encourages the use of information hiding, which is the fundamental principle of object-oriented language that helps to improve software quality. Hence, a member of a type has private accessibility by default. This only has to be modified when that member has to be accessible by any code in the assembly in which it is declared or another assembly that references the member.

Some of the C# constructs like namespace, members of an interface and members of an enumeration have public accessibility and do not allow any other accessibility. In addition, user-defined types that overload specific operators for providing custom implementation have to declare the overloaded operators as public.

Techopedia Explains Public

Public is the most permissive accessibility out of all the other accessibilities, which include private, protected and internal accessibility. This is because there is no restriction on accessing a public type or type member – it can be used for both instance and static members of a type.

For example, BankAccount class represents the account details of a user and can have a public method, DisplayAccountBalance, so that any other class within the assembly or the assembly referencing the class can call this method to display the balance.

In contrast to C++, public members of a base class can only be public in all their derived classes. In C#, there is no option to specify access modifiers, whether protected or private, during derivation of a class.

While public accessibility is used to access a type or its member from a code in the same assembly or another assembly that references it, internal accessibility can be used to access types from within the assembly.

Advertisements

Related Terms

Margaret Rouse

Margaret is an award-winning technical writer and teacher known for her ability to explain complex technical subjects to a non-technical business audience. Over the past twenty years, her IT definitions have been published by Que in an encyclopedia of technology terms and cited in articles by the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine, and Discovery Magazine. She joined Techopedia in 2011. Margaret's idea of a fun day is helping IT and business professionals learn to speak each other’s highly specialized languages.