Boolean.GetTypeCode() Method in C# with Examples

The Boolean.GetTypeCode() method in C# is used to return the type code for the Boolean value type. This method returns TypeCode.Boolean, which is part of the TypeCode enumeration that represents different data types in the .NET framework.

Syntax

Following is the syntax for the Boolean.GetTypeCode() method −

public TypeCode GetTypeCode();

Return Value

This method returns TypeCode.Boolean, which represents the Boolean data type in the TypeCode enumeration.

Using GetTypeCode() with Boolean Values

Example

using System;

public class Demo {
    public static void Main(String[] args) {
        bool val1 = true;
        bool val2 = false;
        
        Console.WriteLine("Value1 = " + val1);
        Console.WriteLine("Value1 (TypeCode) = " + val1.GetTypeCode());
        Console.WriteLine("Value1 (Hashcode) = " + val1.GetHashCode());
        
        Console.WriteLine("\nValue2 = " + val2);
        Console.WriteLine("Value2 (TypeCode) = " + val2.GetTypeCode());
        Console.WriteLine("Value2 (Hashcode) = " + val2.GetHashCode());
    }
}

The output of the above code is −

Value1 = True
Value1 (TypeCode) = Boolean
Value1 (Hashcode) = 1
        
Value2 = False
Value2 (TypeCode) = Boolean
Value2 (Hashcode) = 0

Comparing TypeCode Values

Example

using System;

public class Demo {
    public static void Main(String[] args) {
        bool boolValue = true;
        int intValue = 42;
        string stringValue = "Hello";
        
        TypeCode boolType = boolValue.GetTypeCode();
        TypeCode intType = intValue.GetTypeCode();
        TypeCode stringType = stringValue.GetTypeCode();
        
        Console.WriteLine("Boolean TypeCode: " + boolType);
        Console.WriteLine("Integer TypeCode: " + intType);
        Console.WriteLine("String TypeCode: " + stringType);
        
        Console.WriteLine("\nIs Boolean type? " + (boolType == TypeCode.Boolean));
        Console.WriteLine("TypeCode value: " + (int)boolType);
    }
}

The output of the above code is −

Boolean TypeCode: Boolean
Integer TypeCode: Int32
String TypeCode: String

Is Boolean type? True
TypeCode value: 3

Practical Use Case

Example

using System;

public class Demo {
    public static void CheckDataType(object obj) {
        if (obj is IConvertible convertible) {
            TypeCode typeCode = convertible.GetTypeCode();
            Console.WriteLine($"Object: {obj}, TypeCode: {typeCode}");
            
            if (typeCode == TypeCode.Boolean) {
                Console.WriteLine("This is a Boolean value!");
            }
        }
    }
    
    public static void Main(String[] args) {
        bool flag = true;
        int number = 100;
        
        CheckDataType(flag);
        CheckDataType(number);
    }
}

The output of the above code is −

Object: True, TypeCode: Boolean
This is a Boolean value!
Object: 100, TypeCode: Int32

Conclusion

The Boolean.GetTypeCode() method returns TypeCode.Boolean for any Boolean value, regardless of whether it's true or false. This method is useful for type checking and runtime type identification in applications that work with multiple data types.

Updated on: 2026-03-17T07:04:36+05:30

140 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements