C# Program to Check a Specified Type is Public or not
A class is a collection of methods, variables, and objects. We can create a public class, private class, or protected class using the access modifiers. A class created with the public modifier will have access entirely to a program. So to check whether the given class or type is a public type or not we use the IsPublic property of the Type class. It will return true if the given type is a public type. Otherwise, it will return false. Also, this property will not work with nested types.
Syntax:
public bool IsPublic { get; }
Example 1:
C#
using System;
using System.Reflection;
public class Myclass1
{
public void display()
{
Console.WriteLine( "Hello! GeeksforGeeks" );
}
}
class Myclass2
{
public void Show()
{
Console.WriteLine( "Hey! GeeksforGeeks" );
}
}
public class GFG{
public static void Main( string [] args)
{
Console.WriteLine( typeof (Myclass1).IsPublic);
Console.WriteLine( typeof (Myclass2).IsPublic);
}
}
|
Output:
True
False
Example 2:
C#
using System;
using System.Reflection;
public class Student
{
public void display()
{
Console.WriteLine( "I like C# language alot" );
}
}
public class GFG{
public static void Main( string [] args)
{
if ( typeof (Student).IsPublic == true )
{
Console.WriteLine( "The given class is a public class" );
}
else
{
Console.WriteLine( "The given class is not a public class" );
}
}
}
|
Output:
The given class is a public class
Last Updated :
27 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...