Type.IsPrimitiveImpl Method

Definition

When overridden in a derived class, implements the IsPrimitive property and determines whether the Type is one of the primitive types.

protected abstract bool IsPrimitiveImpl ();

Returns

true if the Type is one of the primitive types; otherwise, false.

Examples

The following example determines whether the given type is a primitive type and displays the result.

using System;
using System.Reflection;
public class MyTypeDelegatorClass : TypeDelegator
{
    public string myElementType = null;
    private Type myType = null ;
    public MyTypeDelegatorClass(Type myType) : base(myType)
    {
        this.myType = myType;
    }
    // Override the IsPrimitiveImpl.
    protected override bool IsPrimitiveImpl()
    {
        // Determine whether the type is a primitive type.
        if(myType.IsPrimitive)
        {
            myElementType = "primitive";
            return true;
        }
        return false;
    }
}
public class MyTypeDemoClass
{
    public static void Main()
    {
        try
        {
            Console.WriteLine ("Determine whether int is a primitive type.");
            MyTypeDelegatorClass myType;
            myType = new MyTypeDelegatorClass(typeof(int));
            // Determine whether int is a primitive type.
            if( myType.IsPrimitive)
            {
                Console.WriteLine(typeof(int) + " is a primitive type.");
            }
            else
            {
                Console.WriteLine(typeof(int) + " is not a primitive type.");
            }
            Console.WriteLine ("\nDetermine whether string is a primitive type.");
            myType = new MyTypeDelegatorClass(typeof(string));
            // Determine if string is a primitive type.
            if( myType.IsPrimitive)
            {
                Console.WriteLine(typeof(string) + " is a primitive type.");
            }
            else
            {
                Console.WriteLine(typeof(string) + " is not a primitive type.");
            }
        }
        catch( Exception e )
        {
            Console.WriteLine("Exception: {0}", e.Message);
        }
    }
}

Remarks

The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Char, Double, and Single.

Applies to

제품 버전
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also