c sharp

C# IsNestedPublic Property

We can create nested classes,nested structure etc in C#. If we want to check if the type is nested with a public modifier or not, IsNestedPublic Property is used. Let’s create nested classes and nested structures and implement IsNestedPublic Property.

IsNestedPublic Property

The IsNestedPublic property from the Type class returns True if the Type is nested and declared as public(modifier). Otherwise, False is returned.

Syntax:

bool typeof(Type).IsNestedPublic

Return Type:

It returns the Boolean value (True/False).

Example 1:

Let’s create a class and check if it is nested public or not.

using System;
         
 //create class - Linuxhint
public class Linuxhint
{
   
    static public  void Main(){
    //check whether the class Linuxhint is nested public or not.
    Console.WriteLine("Is Linuxhint nested public ?:"+typeof(Linuxhint).IsNestedPublic);
}
}

Output:

Explanation:

Here, Linuxhint is a class and declared as public and it is not nested inside any class or any Type. So, False is returned.

Example 2:

Let’s create a class named Linux outside the Linuxhint class and check if it is nested & declared as public in Linuxhint or not.

using System;
         
         
//create class Linux
public class Linux{
    //this is a class
}
 //create class - Linuxhint
class Linuxhint
{
   
    static public  void Main(){
        //check if the class Linux is nested public or not.
    Console.WriteLine("Is Linux nested public in Linuxhint?:"+typeof(Linux).IsNestedPublic);
}
}

Output:

Explanation:

Here, Linux is a class declared as public and it is not nested inside any class or any Type. So, False is returned.

Example 3:

Let’s create a class named Linux inside the Linuxhint class and check if it is nested public in Linuxhint or not.

using System;
       
 //create class - Linuxhint
class Linuxhint
{
   //create class Linux
public class Linux{
    //this is a nested class
}
    static public  void Main(){
        //check if the class Linux is nested public or not.
    Console.WriteLine("Is Linux nested public in Linuxhint?:"+typeof(Linux).IsNestedPublic);
}
}

Output:

Explanation:

Here, Linux is a class declared as public and it is nested inside Linuxhint class. So, True is returned.

Example 4:

Let’s create a structure named Linux inside the Linuxhint class and check if it is nested public in Linuxhint or not.

using System;
       
 //create a class - Linuxhint
class Linuxhint
{
   //create structure - Linux
struct Linux{
    //this is a structure nested inside class
}
    static public  void Main(){
        //check if the class Linux is nested public or not.
    Console.WriteLine("Is Linux structure nested public inside Linuxhint?:"+typeof(Linux).IsNestedPublic);
}
}

Output:

Explanation:

Here, Linux is a structure and it is nested inside Linuxhint class. But Linux is not declared as public, hence False is returned.

Example 5:

Let’s create a structure declared as public named Linux inside the Linuxhint class and check if it is nested public in Linuxhint or not.

using System;
       
 //create a class - Linuxhint
class Linuxhint
{
   //create structure - Linux
public struct Linux{
    //this is a structure nested inside class
}
    static public  void Main(){
        //check if the class Linux is nested public or not.
    Console.WriteLine("Is Linux structure nested public inside Linuxhint?:"+typeof(Linux).IsNestedPublic);
}
}

Output:

Explanation:

Here, Linux is a structure declared as public and it is nested inside Linuxhint class. Hence, True is returned.

Conclusion

Finally, we came to know that IsNestedPublic property is used to check if any Type is nested or not declared by a public modifier.
If any Type is nested inside any Type and declared as public, True is returned. We see five different examples to demonstrate this property.

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