
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Getting the type of the current instance in C#
To get the type of the current instance, the code is as follows −
Example
using System; public class Demo { public static void Main(){ string s = "Demo"; Console.WriteLine("String = " +s); Console.WriteLine("String Type = " +s.GetType()); } }
Output
This will produce the following output −
String = Demo String Type = System.String
Example
Let us now see another example −
using System; public class Demo { public static void Main(){ double val1 = 5.5; int val2 = 10; short val3 = 2; Console.WriteLine("Value = " +val1); Console.WriteLine("Value Type = " +val1.GetType()); Console.WriteLine("Value = " +val2); Console.WriteLine("Value Type = " +val2.GetType()); Console.WriteLine("Value = " +val3); Console.WriteLine("Value Type = " +val3.GetType()); } }
Output
This will produce the following output −
Value = 5.5 Value Type = System.Double Value = 10 Value Type = System.Int32 Value = 2 Value Type = System.Int16
- Related Articles
- Getting the Type of the Tuple’s Element in C#
- Get the HashCode for the current UInt64 instance in C#
- Get the HashCode for the current UInt32 instance in C#
- Get the hash code for the current Int64 instance in C#
- Get the hash code for the current Decimal instance in C#
- Get the underlying type of the current enumeration type C#
- Getting the URL of the current page using Selenium WebDriver.
- Get the fields of the current Type in C#
- Get the members of the current Type in C#
- How to identify the type of a Line instance using FabricJS?
- How to identify the type of an Image instance using FabricJS?
- Get the specified members of the current Type in C#?
- Getting current time in Python
- Getting the unique identifier for the current managed thread in C#
- Get the names of the members of the current enumeration type in C#

Advertisements