Javatpoint Logo
Javatpoint Logo

Boolean.GetTypeCode() Method in C#

In the article, we will describe the role of Boolean.GetTypeCode() Method in C# with its syntax, parameters, and examples.

In C#, the Boolean is a data type used to capture a binary choice, which is often displayed as 'true' or 'false'. The Boolean class has a function to return a TypeCode enumeration value for the currently available object through the GetTypeCode() method. This approach is very effective when we are using dynamic data or when we compare different data types with each other.


It has the following syntax:

Return value:

This method returns the enumerated boolean value.

GetTypeCode() function:

The GetTypeCode() function, which is a part of the Boolean class, can be called on any Boolean type variable or instantiated value. It returns a TypeCode enumeration value, which refers to the actual type of the Boolean. It can be useful for cases in which we desire to execute any Boolean operation without necessarily knowing the type of the Boolean variable.

Example 1:

Let us take a program to implement the Boolean.GetTypeCode() method in C#.


The TypeCode for True is: Boolean


The main class, GetType, defines its Main method, where the entire program execution starts. The main method contains a b1 variable, a boolean that needs to be set to true when first initialized. The TypeCode result of the GetTypeCode() method is assigned to the res variable when called on the b1 variable.

The Console.WriteLine() method will be used to program the display of the type code of the boolean variable b1. It makes use of string interpolation to format the output, where {0} becomes the value of b1, and {1} is replaced by the value of res. The output is boolean. It shows that the type code for the boolean value true is Boolean, which is what we expected.

Example 2:

Let us take another program to implement the Boolean.GetTypeCode() method in C#.


The TypeCode for True is Boolean
The TypeCode for False is Boolean


In this example, the class (GetType) is defined, which contains the Main method where the program execution begins. The type() method is called after the start of the Main() method for both true and false to demonstrate and the GetTypeCode() method.

The type() method belongs outside the Main method because it is defined outside the Main method. Here, the val is a bool parameter. Inside the type() method, the GetTypeCode() method is invoked on the val parameter and the response is saved in the value variable of type TypeCode. The Console.WriteLine() statement shows out the data type of the value val is boolean. It uses string formatting, where {0} is replaced with val value and {1} is replaced with value. The output is "TypeCode of true is Boolean" and "TypeCode of false is Boolean". It shows that the type code for both true and false boolean values became Boolean, which is the expected result.

Advantages of using the Boolean.GetTypeCode() Method:

There are several advantages of the Boolean.GetTypeCode() function in C++. Some main advantages are as follows:

  1. Integration with LINQ Queries: The GetTypeCode() method can be used in LINQ queries, both as a custom filter criterion or as an operation part of the collections' transformation. It can be easy to make the generation of LINQ statements more understandable, and so the expressiveness of the whole LINQ statement itself can be enhanced.
  2. Handling Enumerations: In the situation where we have Boolean states, which can be found in enums (enum Status { Active, Inactive }), we can use a helper method (GetTypeCode()) to differentiate between if it is Boolean or Enumeration type.
  3. Extension Methods for Convenience: Extended methods can be implemented to encapsulate GetTypeCode() logic, making it reusable and accordingly.
  4. Serialization and Deserialization: Subclass serialization of JSON or XML, the GetTypeCode() method can be used to determine which type of data is being deserialized, and action can be taken accordingly.
  5. Type Safety in Dynamic Environments: In a situation where the types of variables can be changed automatically at the runtime, using this method can be an asset to an Olive type safety and the prevention of the unobvious mistakes introduced by a change of types.


In conclusion, the Boolean.GetTypeCode() method in C# is a basic thing in a world where applicability is dynamic, and we have to perform some operations depending on the type of a Boolean variable. It will help the understanding process to be more accessible. Hence, the code will be well-structured and easily developed.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA