C# | Type.HasElementTypeImpl() Method
Last Updated :
22 May, 2019
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:
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
MyClass mytype = new MyClass( typeof ( int ));
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." );
}
}
public class MyClass : TypeDelegator {
public string elementtype = null ;
private Type type = null ;
public MyClass(Type type)
: base (type)
{
this .type = type;
}
protected override bool HasElementTypeImpl()
{
if (type.IsArray) {
elementtype = "array" ;
return true ;
}
if (type.IsByRef)
{
elementtype = "reference" ;
return true ;
}
if (type.IsPointer)
{
elementtype = "pointer" ;
return true ;
}
return false ;
}
}
|
Output:
myArray is not an array, pointer, or reference type.
Example 2:
using System;
using System.Globalization;
using System.Reflection;
class GFG {
public static void Main()
{
MyClass mytype = new MyClass( typeof ( int [,,,,,, ]));
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);
}
}
public class MyClass : TypeDelegator {
public string elementtype = null ;
public Type type = null ;
public MyClass(Type type)
: base (type)
{
this .type = type;
}
protected override bool HasElementTypeImpl()
{
if (type.IsArray)
{
elementtype = "array" ;
return true ;
}
if (type.IsByRef)
{
elementtype = "reference" ;
return true ;
}
if (type.IsPointer)
{
elementtype = "pointer" ;
return true ;
}
return false ;
}
}
|
Output:
The type of System.Int32[,,,,,,] is array.
Reference:
Share your thoughts in the comments
Please Login to comment...