C# | Type.GetDefaultMembers() Method

Type.GetDefaultMembers() Method is used to find the members defined for the current Type whose DefaultMemberAttribute is set.

Syntax: public virtual System.Reflection.MemberInfo[] GetDefaultMembers ();

Return Value: This method returns an array of MemberInfo objects representing all default members of the current Type or an empty array of type MemberInfo, if the current Type does not have default members.

Below programs illustrate the use of Type.GetDefaultMembers() Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetDefaultMembers() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing obj
        object obj = "Ram";
  
        // Getting the type of obj
        // using GetType() Method
        Type type = obj.GetType();
  
        // Getting the DefaultMembers
        // using GetDefaultMembers() Method
        MemberInfo[] info = type.GetDefaultMembers();
  
        // Display the result
        for (int i = 0; i < info.Length; i++)
  
            Console.WriteLine("Result is:  {0}", info[i]);
    }
}

chevron_right


Output:

Result is:  Char Chars [Int32]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetDefaultMembers() Method
using System;
using System.Globalization;
using System.Reflection;
  
// Setting DefaultMemberAttribute
[DefaultMemberAttribute("name")] class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing
        // object of Type dataType
        Type type = typeof(GFG);
  
        // Getting the DefaultMembers
        // using GetDefaultMembers() Method
        MemberInfo[] info = type.GetDefaultMembers();
  
        if (info.Length != 0)
        {
            for (int i = 0; i < info.Length; i++)
                Console.WriteLine("Result is:  {0}", info[i]);
        }
        else {
            Console.WriteLine("DefaultMember is not found");
        }
    }
  
    // Defining Member Attributes
    public void Name(String s) {}
  
    // Defining property
    public String name
    {
        // property or indexer must 
        // have at least one accessor
        get
        {
            return "Ram";
        }
    }
}

chevron_right


Output:

Result is:  System.String name

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.