Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8317745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:42:40+00:00 2026-06-08T21:42:40+00:00

How can I find out what data type some variable is holding? (e.g. int,

  • 0

How can I find out what data type some variable is holding? (e.g. int, string, char, etc.)

I have something like this now:

private static void Main()
{
   var someone = new Person();
   Console.WriteLine(someone.Name.typeOf());
}

public class Person
{
    public int Name { get; set; }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:42:42+00:00Added an answer on June 8, 2026 at 9:42 pm

    Other answers offer good help with this question, but there is an important and subtle issue that none of them addresses directly. There are two ways of considering type in C#: static type and run-time type.

    Static type is the type of a variable in your source code. It is therefore a compile-time concept. This is the type that you see in a tooltip when you hover over a variable or property in your development environment.

    Run-time type is the type of an object in memory. It is therefore a run-time concept. This is the type returned by the GetType() method.

    An object’s run-time type is frequently different from the static type of the variable, property, or method that holds or returns it. For example, you can have code like this:

    object o = "Some string";
    

    The static type of the variable is object, but at run time, the type of the variable’s referent is string. Therefore, the next line will print "System.String" to the console:

    Console.WriteLine(o.GetType()); // prints System.String
    

    But, if you hover over the variable o in your development environment, you’ll see the type System.Object (or the equivalent object keyword).

    For value-type variables, such as int, double, System.Guid, you know that the run-time type will always be the same as the static type, because value types cannot serve as the base class for another type; the value type is guaranteed to be the most-derived type in its inheritance chain. This is also true for sealed reference types: if the static type is a sealed reference type, the run-time value must either be an instance of that type or null.

    Conversely, if the static type of the variable is an abstract type, then it is guaranteed that the static type and the runtime type will be different.

    To illustrate that in code:

    // int is a value type
    int i = 0;
    // Prints True for any value of i
    Console.WriteLine(i.GetType() == typeof(int));
    
    // string is a sealed reference type
    string s = "Foo";
    // Prints True for any value of s
    Console.WriteLine(s == null || s.GetType() == typeof(string));
    
    // object is an unsealed reference type
    object o = new FileInfo("C:\\f.txt");
    // Prints False, but could be true for some values of o
    Console.WriteLine(o == null || o.GetType() == typeof(object));
    
    // FileSystemInfo is an abstract type
    FileSystemInfo fsi = new DirectoryInfo("C:\\");
    // Prints False for all non-null values of fsi
    Console.WriteLine(fsi == null || fsi.GetType() == typeof(FileSystemInfo));
    

    Another user edited this answer to incorporate a function that appears below in the comments, a generic helper method to use type inference to get a reference to a variable’s static type at run time, thanks to typeof:

    Type GetStaticType<T>(T x) => typeof(T);
    

    You can use this function in the example above:

    Console.WriteLine(GetStaticType(o)); // prints System.Object
    

    But this function is of limited utility unless you want to protect yourself against refactoring. When you are writing the call to GetStaticType, you already know that o’s static type is object. You might as well write

    Console.WriteLine(typeof(object)); // also prints System.Object!
    

    This reminds me of some code I encountered when I started my current job, something like

    SomeMethod("".GetType().Name);
    

    instead of

    SomeMethod("String");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data which can be represented by an unsigned Integral type and
Say I have a basic class in Python 3 which represents some number-like data-type.
I know I can find out if a variable is null in Java using
My users pass me an array of some type, say int[] or string[]. I
I can find out the error in my apps. When am trying to execute
Can somebody tell me how I can find out how many threads are in
simple question: How I can find out commands for a DLLImport in C#.Net and
Can anyone tell me how I can find out if an SPField object is
Typically in Django I can find out what queries are being run against the
Is there any way how we can find out the number of prints taken

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.