Open In App

Type.GetConstructors() Method in C# with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

Type.GetConstructors() Method is used to get the constructors of the Type object. There are 2 methods in the overload list of this method as follows:

Type.GetConstructors() Method

This method is used to returns all the public constructors defined for the current Type.
Syntax: 

public System.Reflection.ConstructorInfo[] GetConstructors ();

Returns: This method returns ConstructorInfo array objects representing all the public instance constructors defined for the current Type, but it does not include type initializer(static constructor).
Below programs illustrate the use of Type.GetConstructors() Method:
Example 1:

csharp




// C# program to demonstrate the
// Type.GetConstructors() Method
using System;
using System.Globalization;
using System.Reflection;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
        // Declaring and initializing object
        object obj = new Object();
 
        // Getting the type of object
        // using GetType() method
        Type type = obj.GetType();
 
        // Getting constructors of the current Type.
        // using GetConstructors() Method
        ConstructorInfo[] info = type.GetConstructors();
 
        // Display all the public instance constructors
        Console.WriteLine("All constructors are shown below");
 
        for (int i = 0; i < info.Length; i++)
            Console.WriteLine(info[i]);
    }
}


Output: 

All constructors are shown below
Void .ctor()

 

Example 2:

csharp




// C# program to demonstrate the
// Type.GetConstructors() Method
using System;
using System.Globalization;
using System.Reflection;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
        // Declaring and initializing Type object
        Type type = typeof(string);
 
        // Getting constructors of the current Type.
        // using GetConstructors() Method
        ConstructorInfo[] info = type.GetConstructors();
 
        // Display all the public instance constructors
        Console.WriteLine("All constructors are shown below");
 
        for (int i = 0; i < info.Length; i++)
            Console.WriteLine(info[i]);
    }
}


Output: 

All constructors are shown below
Void .ctor(Char[])
Void .ctor(Char[], Int32, Int32)
Void .ctor(Char*)
Void .ctor(Char*, Int32, Int32)
Void .ctor(SByte*)
Void .ctor(SByte*, Int32, Int32)
Void .ctor(SByte*, Int32, Int32, Encoding)
Void .ctor(Char, Int32)
Void .ctor(ReadOnlySpan`1)

 

Type.GetConstructors(BindingFlags) Method

This method is used to return constructors defined for the current Type, using the specified BindingFlags when overridden in a derived class,
Syntax:
 

public abstract System.Reflection.ConstructorInfo[] GetConstructors (System.Reflection.BindingFlags bindingAttr); 
 

Here, it takes a bitmask comprised of one or more BindingFlags that specify at which point the attend is conducted. or Zero, to return null.
Below are some BindingFlags filter flags that can be used to define which constructors to include in the search:
 

  • You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.
  • Specify BindingFlags.Public to include public constructors in the search.
  • Specify BindingFlags.NonPublic to include non-public constructors (that is, private, internal, and protected constructors) in the search. Constructors of base classes are not returned.

Return Value: This method returns an array of ConstructorInfo objects representing all constructors defined for the current Type that match the specified binding constraints, including the type initializer if it is defined. 
Below programs illustrate the use of the above-discussed method:
Example 1:

csharp




// C# program to demonstrate the
// Type.GetConstructors(BindingFlags)
// Method
using System;
using System.Globalization;
using System.Reflection;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
        // Declaring and initializing Type object
        Type type = typeof(string);
 
        // Declaring and initializing BindingFlags
        // it is used to specify how the search
        // is conducted
        BindingFlags bf = BindingFlags.Public
                          | BindingFlags.Static
                          | BindingFlags.NonPublic
                          | BindingFlags.Instance;
 
        // Getting constructors of the current Type.
        // using GetConstructors() Method
        ConstructorInfo[] info = type.GetConstructors(bf);
 
        // Display all constructors
        Console.WriteLine("All constructors are shown below");
 
        for (int i = 0; i < info.Length; i++)
            Console.WriteLine(info[i]);
    }
}


Output: 

All constructors are shown below
Void .ctor(Char[])
Void .ctor(Char[], Int32, Int32)
Void .ctor(Char*)
Void .ctor(Char*, Int32, Int32)
Void .ctor(SByte*)
Void .ctor(SByte*, Int32, Int32)
Void .ctor(SByte*, Int32, Int32, Encoding)
Void .ctor(Char, Int32)
Void .ctor(ReadOnlySpan`1)

 

Example 2:

csharp




// C# program to demonstrate the
// Type.GetConstructors(BindingFlags)
// Method
using System;
using System.Globalization;
using System.Reflection;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
        // Creating and initializing object
        object obj = new Object();
 
        // Declaring and initializing Type object
        Type type = obj.GetType();
 
        // Declaring and initializing BindingFlags
        // it is used to specify how the search is conducted
        BindingFlags bf = BindingFlags.Public
                          | BindingFlags.Static
                          | BindingFlags.NonPublic
                          | BindingFlags.Instance;
 
        // Getting constructors of the current Type.
        // using GetConstructors() Method
        ConstructorInfo[] info = type.GetConstructors(bf);
 
        // Display all constructors
        Console.WriteLine("All constructors are shown below");
 
        for (int i = 0; i < info.Length; i++)
            Console.WriteLine(info[i]);
    }
}


Output: 

All constructors are shown below
Void .ctor()

 

Reference:



Last Updated : 03 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads