c sharp

IsNotPublic Property

By specifying the class as Public, it is possible to access all the members of a class at any place of the code. If we want to access only some classes, we can create a class without a public modifier.

In C#, if we want to check if the Type is Public or not, the IsNotPublic property is used.

IsNotPublic Property

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

Syntax:

typeof(Type).IsNotPublic

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 Not Public Types.

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 non-public?: "+ typeof(Food).IsNotPublic);

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

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

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

}
}

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, True 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 non-public?: "+ typeof(Class1).IsNotPublic);

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

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

}
}

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 IsNotPublic property.

They are Public so for all of them False is returned.

Closing

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

Happy Coding

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