FieldInfo.IsPrivate Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets a value indicating whether the field is private.

Namespace:   System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

public bool IsPrivate { get; }

Property Value

Type: System.Boolean

true if the field is private; otherwise; false.

Private fields are accessible only from member functions.

The IsPrivate property is set when the FieldAttributes.Private attribute is set.

To get the IsPrivate property, first get the class Type. From the Type, get the FieldInfo. From the FieldInfo, get the IsPrivate property. To access a non-public field, set the BindingFlags to NonPublic, and either Static or Instance in the GetField method.

The following example returns a value indicating whether or not the field of the class is private.

using System;
using System.Reflection;

class MyClass
    private string myField;
    public string[] myArray = new string[] {"New York", "New Jersey"};
        myField = "Microsoft";
    string GetField
            return myField;

class FieldInfo_IsPrivate
    public static void Main()
            // Gets the type of MyClass.
            Type myType = typeof(MyClass);

            // Gets the field information of MyClass.
            FieldInfo[] myFields = myType.GetFields(BindingFlags.NonPublic

            Console.WriteLine("\nDisplaying whether the fields of {0} are private or not:\n", myType);
            for(int i = 0; i < myFields.Length; i++)
                // Check whether the field is private or not. 
                    Console.WriteLine("{0} is a private field.", myFields[i].Name);
                    Console.WriteLine("{0} is not a private field.", myFields[i].Name);
        catch(Exception e)
            Console.WriteLine("Exception : {0} " , e.Message);

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top