How to use the GetType method of array class in C#?


The GetType() method of array class in C# gets the Type of the current instance (Inherited from Object).

To get the type.

Type tp = value.GetType();

In the below example, we are checking the int value using the type.

if (tp.Equals(typeof(int)))
Console.WriteLine("{0} is an integer data type.", value)

The following is the usage of GetType() method in C#.

Example

using System
public class Program {
   public static void Main() {
      object[] values = { (int) 100, (long) 17111};
      foreach (var value in values) {
         Type tp = value.GetType();
         if (tp.Equals(typeof(int)))
         Console.WriteLine("{0} is aninteger data type.", value);
         else
         Console.WriteLine("'{0}' is not an int data type.", value);
      }
   }
}

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements