Type.GetFields() Method in C#

The Type.GetFields() method in C# is used to retrieve the fields of the current Type using reflection. This method returns an array of FieldInfo objects representing the fields that match the specified binding criteria.

Fields are data members of a class that store values. The GetFields() method allows you to examine both public and non-public fields at runtime, making it useful for inspection, debugging, and dynamic programming scenarios.

Syntax

Following is the syntax for the parameterless overload −

public System.Reflection.FieldInfo[] GetFields();

Following is the syntax for the overload with binding flags −

public System.Reflection.FieldInfo[] GetFields(BindingFlags bindingAttr);

Parameters

bindingAttr: A bitwise combination of BindingFlags values that specify how the search is conducted. Common flags include Public, NonPublic, Static, and Instance.

Return Value

Returns an array of FieldInfo objects representing the fields that match the specified binding flags. If no fields are found, an empty array is returned.

GetFields() with BindingFlags Type.GetFields(BindingFlags) Retrieves fields based on specified criteria Public Public fields only Static Static fields only Instance Instance fields only NonPublic Private/ Protected

Using GetFields() with String Type

Example

using System;
using System.Reflection;

public class Demo {
   public static void Main(){
      Type type = typeof(System.String);
      FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic);
      Console.WriteLine("Following are the non-public static fields:");
      foreach (FieldInfo myField in fields){
         Console.WriteLine(myField.ToString());
      }
   }
}

The output of the above code is −

Following are the non-public static fields:
Int32 TrimHead
Int32 TrimTail
Int32 TrimBoth
Int32 charPtrAlignConst
Int32 alignConst

Using GetFields() with Custom Class

Example

using System;
using System.Reflection;

public class Student {
   public string name;
   private int age;
   public static string school = "ABC School";
   private static int totalStudents = 100;
}

public class Demo {
   public static void Main(){
      Type type = typeof(Student);
      
      Console.WriteLine("All public fields:");
      FieldInfo[] publicFields = type.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
      foreach (FieldInfo field in publicFields){
         Console.WriteLine($"{field.FieldType.Name} {field.Name}");
      }
      
      Console.WriteLine("\nAll private fields:");
      FieldInfo[] privateFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
      foreach (FieldInfo field in privateFields){
         Console.WriteLine($"{field.FieldType.Name} {field.Name}");
      }
   }
}

The output of the above code is −

All public fields:
String name
String school

All private fields:
Int32 age
Int32 totalStudents

Using GetFields() with Char Type

Example

using System;
using System.Reflection;

public class Demo {
   public static void Main(){
      Type type = typeof(System.Char);
      FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic);
      Console.WriteLine("Following are the non-public static fields:");
      foreach (FieldInfo myField in fields){
         Console.WriteLine(myField.ToString());
      }
   }
}

The output of the above code is −

Following are the non-public static fields:
Byte[] categoryForLatin1
Int32 UNICODE_PLANE00_END
Int32 UNICODE_PLANE01_START
Int32 UNICODE_PLANE16_END
Int32 HIGH_SURROGATE_START
Int32 LOW_SURROGATE_END

Common BindingFlags Combinations

BindingFlags Combination Description
BindingFlags.Public | BindingFlags.Instance Gets all public instance fields
BindingFlags.NonPublic | BindingFlags.Instance Gets all private/protected instance fields
BindingFlags.Public | BindingFlags.Static Gets all public static fields
BindingFlags.NonPublic | BindingFlags.Static Gets all private/protected static fields

Conclusion

The Type.GetFields() method is a powerful reflection tool that allows you to inspect the fields of any type at runtime. By using different BindingFlags combinations, you can control which fields are returned based on their visibility and whether they are static or instance members.

Updated on: 2026-03-17T07:04:35+05:30

366 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements