c sharp

C# IsPublic Property

By specifying the class as Public, it is possible to access all the members of a class at any place of the code. In C#, if we want to check if the Type is Public or not, the IsPublic property is used.

IsPublic Property

The IsPublic property from the Type class returns True, if the Type is specified as public. Otherwise, False is returned.

Syntax:

typeof(Type).IsPublic

Return Type:

It returns the Boolean value (True/False).

Example 1:

Let’s create an Abstract class, Interface, and Enum and check if these are Public Types or not.

using System;

//create abstract class
         abstract class Food{
             //this is abstract class
         }
         
         
//create interface
         interface Phone{
             //this is an interface
         }

//create Enum
         enum Orders{
             value1,value2
             //this is an enum
         }      
         
class Linuxhint
{
   
    static public  void Main(){
       
 //check the Food class is public or not
Console.WriteLine("Is Food class public?: "+ typeof(Food).IsPublic);

//check the Linuxhint class is public or not
Console.WriteLine("Is Linuxhint class public?: "+ typeof(Linuxhint).IsPublic);

//check the Phone is public or not
Console.WriteLine("Is Phone public?: "+ typeof(Phone).IsPublic);

//check the Orders enum is public or not
Console.WriteLine("Is Orders enum public?: "+ typeof(Orders).IsPublic);

}
}

Output:

Explanation:

Line 3-6:

Abstract Class creation:

Line 9-12:

Create Interface

Line 14-18:

Create an Enum with two values.

Line 23 – 35:

Inside Main method, check all the above three Public or not.

As we saw that without specifying public, the Types are not Public. So, False is returned.

Example 2:

Let’s create three classes with public identifiers and check if they are public or not.

using System;

//create public class - Class1
         public class Class1{
             //this is first class
         }
         
         
//create public class - Class2
         public class Class2{
             //this is second class
         }

//create public class - Class3
         public class Class3{
             //this is third class
         }      
         
class Linuxhint
{
   
    static public  void Main(){
       
 //check the Class1 is public or not
Console.WriteLine("Is Class1 public?: "+ typeof(Class1).IsPublic);

//check the Class2 is public or not
Console.WriteLine("Is Class2 public?: "+ typeof(Class2).IsPublic);

//check the Class3 is public or not
Console.WriteLine("Is Class3 public?: "+ typeof(Class3).IsPublic);

}
}

Output:

Explanation:

Line 3 to 17:

We created three public classes named Class1,Class2 and Class3.

Line 18 to 22:

Let’s check if these are Public or not using IsPublic property.

They are Public. So, for all of them True is returned.

Closing

In this C# tutorial, we saw how to check if the Type is Public or not using the IsPublic property. This property tells us by returning a Boolean value with two examples. If it is True, we can say that the Type is Public and If it is False, we can say that the type is not Public.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain