Object.GetType Method (System) (2024)

Table of Contents
Examples Remarks Applies to See also

Gets the Type of the current instance.

public: Type ^ GetType();
public Type GetType ();
member this.GetType : unit -> Type
Public Function GetType () As Type

Returns

Type

The exact runtime type of the current instance.

Examples

The following code example demonstrates that GetType returns the runtime type of the current instance.

using namespace System;public ref class MyBaseClass {};public ref class MyDerivedClass: MyBaseClass{};int main(){ MyBaseClass^ myBase = gcnew MyBaseClass; MyDerivedClass^ myDerived = gcnew MyDerivedClass; Object^ o = myDerived; MyBaseClass^ b = myDerived; Console::WriteLine( "mybase: Type is {0}", myBase->GetType() ); Console::WriteLine( "myDerived: Type is {0}", myDerived->GetType() ); Console::WriteLine( "object o = myDerived: Type is {0}", o->GetType() ); Console::WriteLine( "MyBaseClass b = myDerived: Type is {0}", b->GetType() );}/*This code produces the following output.mybase: Type is MyBaseClassmyDerived: Type is MyDerivedClassobject o = myDerived: Type is MyDerivedClassMyBaseClass b = myDerived: Type is MyDerivedClass */
using System;public class MyBaseClass {}public class MyDerivedClass: MyBaseClass {}public class Test{ public static void Main() { MyBaseClass myBase = new MyBaseClass(); MyDerivedClass myDerived = new MyDerivedClass(); object o = myDerived; MyBaseClass b = myDerived; Console.WriteLine("mybase: Type is {0}", myBase.GetType()); Console.WriteLine("myDerived: Type is {0}", myDerived.GetType()); Console.WriteLine("object o = myDerived: Type is {0}", o.GetType()); Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType()); }}// The example displays the following output:// mybase: Type is MyBaseClass// myDerived: Type is MyDerivedClass// object o = myDerived: Type is MyDerivedClass// MyBaseClass b = myDerived: Type is MyDerivedClass
type MyBaseClass() = class endtype MyDerivedClass() = inherit MyBaseClass()let myBase = MyBaseClass()let myDerived = MyDerivedClass()let o: obj = myDerivedlet b: MyBaseClass = myDerivedprintfn $"mybase: Type is {myBase.GetType()}"printfn $"myDerived: Type is {myDerived.GetType()}"printfn $"object o = myDerived: Type is {o.GetType()}"printfn $"MyBaseClass b = myDerived: Type is {b.GetType()}"// The example displays the following output:// mybase: Type is MyBaseClass// myDerived: Type is MyDerivedClass// object o = myDerived: Type is MyDerivedClass// MyBaseClass b = myDerived: Type is MyDerivedClass
' Define a base and a derived class.Public Class MyBaseClassEnd Class Public Class MyDerivedClass : Inherits MyBaseClassEnd Class Public Class Test Public Shared Sub Main() Dim base As New MyBaseClass() Dim derived As New MyDerivedClass() Dim o As Object = derived Dim b As MyBaseClass = derived Console.WriteLine("base.GetType returns {0}", base.GetType()) Console.WriteLine("derived.GetType returns {0}", derived.GetType()) Console.WriteLine("Dim o As Object = derived; o.GetType returns {0}", o.GetType()) Console.WriteLine("Dim b As MyBaseClass = derived; b.Type returns {0}", b.GetType()) End Sub End Class ' The example displays the following output:' base.GetType returns MyBaseClass' derived.GetType returns MyDerivedClass' Dim o As Object = derived; o.GetType returns MyDerivedClass' Dim b As MyBaseClass = derived; b.Type returns MyDerivedClass

Remarks

Because System.Object is the base class for all types in the .NET type system, the GetType method can be used to return Type objects that represent all .NET types. .NET recognizes the following five categories of types:

  • Classes, which are derived from System.Object,

  • Value types, which are derived from System.ValueType.

  • Interfaces, which are derived from System.Object starting with the .NET Framework 2.0.

  • Enumerations, which are derived from System.Enum.

  • Delegates, which are derived from System.MulticastDelegate.

For two objects x and y that have identical runtime types, Object.ReferenceEquals(x.GetType(),y.GetType()) returns true. The following example uses the GetType method with the ReferenceEquals method to determine whether one numeric value is the same type as two other numeric values.

int n1 = 12;int n2 = 82;long n3 = 12;Console.WriteLine("n1 and n2 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n2.GetType()));Console.WriteLine("n1 and n3 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n3.GetType()));// The example displays the following output:// n1 and n2 are the same type: True// n1 and n3 are the same type: False
open Systemlet n1 = 12let n2 = 82let n3 = 12Lprintfn $"n1 and n2 are the same type: {Object.ReferenceEquals(n1.GetType(), n2.GetType())}"printfn $"n1 and n3 are the same type: {Object.ReferenceEquals(n1.GetType(), n3.GetType())}"// The example displays the following output:// n1 and n2 are the same type: True// n1 and n3 are the same type: False
Module Example Public Sub Main() Dim n1 As Integer = 12 Dim n2 As Integer = 82 Dim n3 As Long = 12 Console.WriteLine("n1 and n2 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n2.GetType())) Console.WriteLine("n1 and n3 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n3.GetType())) End SubEnd Module' The example displays the following output:' n1 and n2 are the same type: True' n1 and n3 are the same type: False

Note

To determine whether an object is a specific type, you can use your language's type comparison keyword or construct. For example, you can use the TypeOf…Is construct in Visual Basic or the is keyword in C#.

The GetType method is inherited by all types that derive from Object. This means that, in addition to using your own language's comparison keyword, you can use the GetType method to determine the type of a particular object, as the following example shows.

object[] values = { (int) 12, (long) 10653, (byte) 12, (sbyte) -5, 16.3, "string" };foreach (var value in values) { Type t = value.GetType(); if (t.Equals(typeof(byte))) Console.WriteLine("{0} is an unsigned byte.", value); else if (t.Equals(typeof(sbyte))) Console.WriteLine("{0} is a signed byte.", value); else if (t.Equals(typeof(int))) Console.WriteLine("{0} is a 32-bit integer.", value); else if (t.Equals(typeof(long))) Console.WriteLine("{0} is a 64-bit integer.", value); else if (t.Equals(typeof(double))) Console.WriteLine("{0} is a double-precision floating point.", value); else Console.WriteLine("'{0}' is another data type.", value);}// The example displays the following output:// 12 is a 32-bit integer.// 10653 is a 64-bit integer.// 12 is an unsigned byte.// -5 is a signed byte.// 16.3 is a double-precision floating point.// 'string' is another data type.
let values: obj[] = [| 12; 10653L; 12uy -5y; 16.3; "string" |]for value in values do let t = value.GetType() if t.Equals typeof<byte> then printfn $"{value} is an unsigned byte." elif t.Equals typeof<sbyte> then printfn $"{value} is a signed byte." elif t.Equals typeof<int> then printfn $"{value} is a 32-bit integer." elif t.Equals typeof<int64> then printfn $"{value} is a 64-bit integer." elif t.Equals typeof<double> then printfn $"{value} is a double-precision floating point." else printfn $"'{value}' is another data type."// The example displays the following output:// 12 is a 32-bit integer.// 10653 is a 32-bit integer.// 12 is an unsigned byte.// -5 is a signed byte.// 16.3 is a double-precision floating point.// 'string' is another data type.
Module Example Public Sub Main() Dim values() As Object = { 12, CLng(10653), CByte(12), CSbyte(-5), 16.3, "string" } For Each value In values Dim t AS Type = value.GetType() If t.Equals(GetType(Byte)) Console.WriteLine("{0} is an unsigned byte.", value) ElseIf t.Equals(GetType(SByte)) Console.WriteLine("{0} is a signed byte.", value) ElseIf t.Equals(GetType(Integer)) Console.WriteLine("{0} is a 32-bit integer.", value) ElseIf t.Equals(GetType(Long)) Console.WriteLine("{0} is a 64-bit integer.", value) ElseIf t.Equals(GetType(Double)) Console.WriteLine("{0} is a double-precision floating point.", value) Else Console.WriteLine("'{0}' is another data type.", value) End If Next End SubEnd Module' The example displays the following output:' 12 is a 32-bit integer.' 10653 is a 32-bit integer.' 12 is an unsigned byte.' -5 is a signed byte.' 16.3 is a double-precision floating point.' 'string' is another data type.

The Type object exposes the metadata associated with the class of the current Object.

Applies to

See also

  • Type
Object.GetType Method (System) (2024)
Top Articles
Reasons You Should Shower After Swimming In A Pool
The 9 most common software testing mistakes you should avoid
MyChart - Baptist Health
Kelbi Horn
My Chart Saint Alphonsus
Quatre questions sur Temu, l'application chinoise de e-commerce qui cartonne malgré des accusations d'espionnage
Q-global Web-based Administration, Scoring, and Reporting
Comment créer et interpréter un tracé Q-Q dans R - Statorials
Jet Ski Rental Conneaut Lake Pa
Www.adultswim.com Activate
Life And Wealth Mastery Fiji Cost
Pocket Edition Minecraft Pocket Edition Manual Pdf
Sarah Colman-Livengood Park Raytown Photos
48 Hours Season 35 Episodes
18002226885
Emiddio Botta Obituary
Word Trip Level 92
National Weather Service Cody Wyoming
Printable Coupon $3 Off Pull-Ups
t-Test, Chi-Square, ANOVA, Regression, Correlation...
Tcp Cypresswood
Movie Tavern Suwanee Menu
Opel Rocks-e im Test: Cooler Köder
Nick Avocado Butthole
Po Box 30425 Salt Lake City
Foley Housing Authority Photos
Cbs Scores Mlb
Whisk Recipe Calculator
Deep Rock Galactic How to Level Up Fast
Craigslist Colville Wa Rentals
Caroline Cps.powerschool.com
Where To Buy Patti Labelle Sweet Potato Pie - PieProNation.com
House Party 2023 Showtimes Near Cinemark Oakley Station And Xd
Bostick Tompkins Obituaries
Reviewing the Reviews: News4 I-Team Finds Online Industry Designed to Deceive You
The Divergent Series: Insurgent - Wikiquote
Sapphire Community Portal Southwestern
Gho Inventory Homes Vero Beach
What Website Assists The Educational Services Officer
Renfield Showtimes Near Cinemark North Haven And Xd
Walgreens Pharmacy Customer Service Associate in BRONX, New York, United States
Albertville Memorial Funeral Home Obituaries
Victoria Medlin Cause Of Death
Klein Isd 2023-24 Calendar
Humanplex
Exercices corrigés -Différents types de raisonnement : absurde, contraposée, récurrence, analyse-synthèse...
Craigslist Farm And Garden - By Owner Nebraska
Weather Underground Merritt Island
8.7 Increase Of 841
Dr. David Oualaalou Ethnicity
Mals Crazy Crab
Round Cake Pans Walmart
Latest Posts
Article information

Author: Tish Haag

Last Updated:

Views: 6412

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Tish Haag

Birthday: 1999-11-18

Address: 30256 Tara Expressway, Kutchburgh, VT 92892-0078

Phone: +4215847628708

Job: Internal Consulting Engineer

Hobby: Roller skating, Roller skating, Kayaking, Flying, Graffiti, Ghost hunting, scrapbook

Introduction: My name is Tish Haag, I am a excited, delightful, curious, beautiful, agreeable, enchanting, fancy person who loves writing and wants to share my knowledge and understanding with you.