C# | Type.HasElementTypeImpl() Method

Type.HasElementTypeImpl() Method is used when overridden in a derived class, implementing the HasElementType property and determines whether the current Type encompasses or refers to another type. It means this method checks whether the current Type is an array, a pointer, or is passed by reference.

Syntax: protected abstract bool HasElementTypeImpl ();

Return Value: This method returns true if the Type is an array, a pointer, or is passed by reference otherwise, false.



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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.HasElementTypeImpl() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // creating and initializing object of MyClass
        MyClass mytype = new MyClass(typeof(int));
  
        // checking if mytype has any elementtype or not
        if (mytype.HasElementType)
            Console.WriteLine("The type of myArray is {0}.", mytype.elementtype);
        else
            Console.WriteLine("myArray is not an array, pointer, or reference type.");
    }
}
  
// Defining MyClass extended from TypeDelegator
public class MyClass : TypeDelegator {
  
    // creating and initializing elementtype with null
    public string elementtype = null;
  
    // creating and initializing type with null
    private Type type = null;
  
    // Constructor
    public MyClass(Type type)
                 : base(type)
    {
        this.type = type;
    }
  
    // Override Type.HasElementTypeImpl().
    protected override bool HasElementTypeImpl()
    {
  
        // Determine whether the type is an array.
        if (type.IsArray) {
            elementtype = "array";
            return true;
        }
  
        // Determine whether the type is a reference.
        if (type.IsByRef) 
        {
            elementtype = "reference";
            return true;
        }
  
        // Determine whether the type is a pointer.
        if (type.IsPointer)
        {
            elementtype = "pointer";
            return true;
        }
  
        // Return false if the type is not
        // a reference, array, or pointer type.
        return false;
    }
}

chevron_right


Output:

myArray is not an array, pointer, or reference type.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.HasElementTypeImpl() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // creating and initializing object of MyClass
        MyClass mytype = new MyClass(typeof(int[,,,,,, ]));
  
        // checking if mytype has any elementtype or not
        if (mytype.HasElementType)
            Console.WriteLine("The type of {1} is {0}.",
                       mytype.elementtype, mytype.type);
        else
            Console.WriteLine("{0} is not an array, pointer, or reference type.",
                                                                    mytype.type);
    }
}
  
// Defining MyClass extended from TypeDelegator
public class MyClass : TypeDelegator {
  
    // creating and initializing elementtype with null
    public string elementtype = null;
  
    // creating and initializing type with null
    public Type type = null;
  
    // Constructor
    public MyClass(Type type)
                : base(type)
    {
        this.type = type;
    }
  
    // Override Type.HasElementTypeImpl().
    protected override bool HasElementTypeImpl()
    {
        // Determine whether the type is an array.
        if (type.IsArray) 
        {
  
            elementtype = "array";
            return true;
        }
  
        // Determine whether the type is a reference.
        if (type.IsByRef) 
        {
            elementtype = "reference";
            return true;
        }
  
        // Determine whether the type is a pointer.
        if (type.IsPointer) 
        {
            elementtype = "pointer";
            return true;
        }
  
        // Return false if the type is not a 
        // reference, array, or a pointer type
        return false;
    }
}

chevron_right


Output:

The type of System.Int32[,,,,,,] is array.

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.