How can I get the data type of a variable in C#?

前端 未结 8 822
忘了有多久
忘了有多久 2020-11-27 04:32

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

I have something like this now:

using System;
using System         


        
8条回答
  •  [愿得一人]
    2020-11-27 05:21

    check out one of the simple way to do this

    // Read string from console
            string line = Console.ReadLine(); 
            int valueInt;
            float valueFloat;
            if (int.TryParse(line, out valueInt)) // Try to parse the string as an integer
            {
                Console.Write("This input is of type Integer.");
            }
            else if (float.TryParse(line, out valueFloat)) 
            {
                Console.Write("This input is of type Float.");
            }
            else
            {
                Console.WriteLine("This input is of type string.");
            }
    

提交回复
热议问题